文件句柄对父进程打开,但对每个子进程都关闭

时间:2014-12-17 17:26:31

标签: python linux multiprocessing python-multiprocessing

我写了这个小例子:

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

1 个答案:

答案 0 :(得分:1)

它与传递文件作为参数有关。将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
相关问题