我写了这个小例子:
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()
这将打印:
False
2 True
3 True
1 True
4 True
False
done
我的问题是为什么关闭子进程的文件句柄,以及如何使它们保持打开状态,以便每个进程都可以处理该文件?
由于在评论中被问到:
$ 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
它与将文件作为参数传递有关。将 fp 更改为文件
import multiprocessing
from functools import partial
def foo(x, fp):
print str(x) + " "+ str(file.closed)
return
if __name__=='__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"
输出
False
1 False
2 False
3 False
4 False
False
done