I have a function and inside of it I'm using os.fork():
def translating(words):
'''Translate words using API'''
number_of_threads = 3
t = [] #list to store translated words
l = len(words_words)
n = (l - (l % number_of_threads)) / number_of_threads #number of words for each PID I will create
for thread_number in range(number_of_threads):
pid = os.fork()
if pid != 0:
'''print('Process {0} spawned' .format(pid))'''
else:
for i in words[thread_number*n:thread_number*n+n]: #slice of words for each PID
translation = translate.translate(i, 'en')['text'] #API returns list of translated words
for a in translation:
t.append(a)
os._exit(0)
return t
My problem is 'return t' happens right after the second (from 3) PID is created. Before 'for-loops' finish iteration. Im stuck and have absolutely no idea why does this happen... What am I doing wrong?