使用multiprocessing.Pool打开的文件太多

时间:2011-07-22 13:44:54

标签: python file macos

我有一个运行multiprocessing.Pool的python脚本来分别处理很多文件。我的cpu限制通常为8.我的问题是在运行一段时间后我总是得到“IOError:[Errno 24]太多打开的文件”。每个子进程打开一些文件,只能使用file.open()进行读取。然后将这些文件处理程序传递给多个函数以检索数据。在每个子进程结束时,这些文件将使用file.close()关闭。我也尝试了with语句,但没有解决问题。有没有人知道什么是错的。我用Google搜索,但没有找到任何答案。我正在关闭文件,函数正在正常返回,这样可以保留文件处理程序。

我的设置是使用python 2.6的Mac 10.5

由于

奥根

    from custom import func1, func2
    # func1 and func2 only seek, read and return values form the file
    # however, they do not close the file
    import multiprocessing
    def Worker(*args):
        f1 = open("db1.txt")
        f2 = open("db2.txt")
        for each in args[1]:
            # do many stuff
            X = func1(f1)
            Y = func2(f2)

        f1.close()
        f2.close()
        return

    Data = {1:[2], 2:[3]}  
    JobP= multiprocessing.Pool(8) 
    jobP.map_async(Worker, Data.items()) 
    jobP.close()
    jobP.join()

2 个答案:

答案 0 :(得分:0)

您可能受到操作系统的打开文件限制的限制。有关详细信息,请参阅How do I change the number of open files limit in Linux?。我个人更喜欢更改/etc/security/limits.conf设置。

答案 1 :(得分:0)

更改Yosemite中的打开文件数限制(OS X 10.10):

sudo launchctl limit maxfiles [number-of-files] unlimited