使用多处理打开的文件太多。P

2024-10-01 00:31:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个运行的python脚本多处理.池单独处理大量文件。我通常cpu的限制是8。我的问题是运行一段时间后,我总是得到“IOError:[Errno 24]打开的文件太多”。每个子进程都会打开一些文件,以便只使用文件.打开(). 然后将这些文件处理程序传递给多个函数来检索数据。在每个子进程结束时,这些文件将用文件.close(). 我也尝试了with语句,但没有解决问题。有人知道怎么回事吗。我搜索了一下,但没有找到任何答案。我正在关闭文件,函数返回正确,所以什么保留了文件处理程序。在

我的设置是mac10.5和python2.6

谢谢

欧甘

    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()

Tags: and文件the函数import处理程序closereturn
2条回答

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

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

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

相关问题 更多 >