I have written this small example:
import multiprocessing
from functools import partial
def foo(x, fp):
print str(x) + " "+ str(fp.closed)
return
def main():
with open("test.txt", 'r') as file:
pool = multiprocessing.Pool(multiprocessing.cpu_count())
partial_foo = partial(foo, fp=file)
print file.closed
pool.map(partial_foo, [1,2,3,4])
pool.close()
pool.join()
print file.closed
print "done"
if __name__=='__main__':
main()
Which will print:
False
2 True
3 True
1 True
4 True
False
done
My question would be why are file handles closed for the child process and how would I keep them open such that every process can work with the file?
Since it was asked in the comments:
$ uname -a && python2.7 -V
Linux X220 3.17.6-1-ARCH #1 SMP PREEMPT Sun Dec 7 23:43:32 UTC 2014 x86_64 GNU/Linux
Python 2.7.9