I have found some similar questions to mine on this forum but none that has helped with my specific problem (I'm fairly new to python so forgive me if I should've understood the answer from a different thread). I am actually working with a fairly complicated python code, but to illustrate the problem I've having I have written a very minimal code that exhibits the same behavior:
from multiprocessing import Pool
log_output = open('log','w')
def sum_square(number):
s = 0
for i in range(number):
s += i * i
log_output.write(str(s)+'\n')
return s
if __name__ == "__main__":
numbers = range(5)
p = Pool()
result = p.map(sum_square,numbers)
p.close()
p.join()
When this code is executed, there are no errors or warnings, etc., but the resulting file "log" is always totally empty. This is the same behavior I find in my other more complicated python program also using a multiprocessing pool. I had expected that the processes would probably print to file in the wrong order, but I'm confused why nothing prints at all. If I get rid of the multiprocessing and just run the same basic code with a single process, it prints to file just fine. Can anyone help me understand what I'm doing wrong here? Thanks.