gpt4 book ai didi

Python 子进程用完文件描述符

转载 作者:太空狗 更新时间:2023-10-29 23:55:54 31 4
gpt4 key购买 nike

我有一个长期运行的 python 项目,它使用 subprocess 模块来启动各种其他程序。它等待每个程序完成,然后结束包装函数并返回到它的等待循环。

最终,这会使运行它的计算机停止运行,并出现没有更多文件描述符可用的错误。

我无法在 subprocess docs 中找到任何地方当子进程关闭时文件描述符会发生什么。起初,我以为它们会自动关闭,因为 subprocess.call() 命令会一直等到子进程终止。

但如果是那样的话,我就不会有问题了。我还认为,如果还有遗留的东西,python 会在函数完成时对其进行垃圾回收,并且文件描述符超出范围。但事实似乎也并非如此。

我如何才能访问这些文件描述符? subprocess.call() 函数只返回退出代码,不返回打开的文件描述符。我还缺少其他东西吗?

该项目充当各种企业应用程序之间的粘合剂。所述应用程序不能流水线化,它们是 gui 系统。所以,我唯一能做的就是用内置的宏启动它们。这些宏输出文本文件,我将其用于管道中的下一个程序。

是的,它和听起来一样糟糕。幸运的是,所有文件最终都有非常独特的名称。因此,在接下来的几天里,我将使用下面建议的 sys internals 工具来尝试追踪该文件。我会告诉你结果如何。

大多数文件我不打开,我只是用 win32file.CopyFile() 函数移动它们。

最佳答案

我遇到了同样的问题。

我们经常使用 subprocess.Popen() 在 Windows 环境中调用外部工具。在某些时候,我们遇到了没有更多文件描述符可用的问题。我们深入研究了这个问题,发现 subprocess.Popen 实例在 Windows 中的行为与在 Linux 中不同。

如果 Popen 实例未被销毁(例如,通过某种方式保留引用,从而不允许垃圾收集器销毁对象),调用期间创建的管道在 Windows 中保持打开状态,而在 Linux 中它们会自动打开在调用 Popen.communicate() 后关闭。如果在进一步的调用中继续这样做,来自管道的“僵尸”文件描述符将堆积起来,并最终导致 Python 异常 IOError: [Errno 24] Too many open files

如何在 Python 中获取打开的文件描述符

为了解决我们的问题,我们需要一种方法来获取 Python 脚本中的有效文件描述符。因此,我们制作了以下脚本。请注意,我们只检查从 0 到 100 的文件描述符,因为我们不会同时打开这么多文件。

fd_table_status.py:

import os
import stat

_fd_types = (
('REG', stat.S_ISREG),
('FIFO', stat.S_ISFIFO),
('DIR', stat.S_ISDIR),
('CHR', stat.S_ISCHR),
('BLK', stat.S_ISBLK),
('LNK', stat.S_ISLNK),
('SOCK', stat.S_ISSOCK)
)

def fd_table_status():
result = []
for fd in range(100):
try:
s = os.fstat(fd)
except:
continue
for fd_type, func in _fd_types:
if func(s.st_mode):
break
else:
fd_type = str(s.st_mode)
result.append((fd, fd_type))
return result

def fd_table_status_logify(fd_table_result):
return ('Open file handles: ' +
', '.join(['{0}: {1}'.format(*i) for i in fd_table_result]))

def fd_table_status_str():
return fd_table_status_logify(fd_table_status())

if __name__=='__main__':
print fd_table_status_str()

简单运行时,它会显示所有打开的文件描述符及其各自的类型:

$> python fd_table_status.py
Open file handles: 0: CHR, 1: CHR, 2: CHR
$>

通过Python代码调用fd_table_status_str()输出结果是一样的。有关“CHR”和尊重“短代码”含义的详细信息,请参阅 Python documentation on stat .

测试文件描述符行为

尝试在 Linux 和 Windows 中运行以下脚本:

test_fd_handling.py:

import fd_table_status
import subprocess
import platform

fds = fd_table_status.fd_table_status_str

if platform.system()=='Windows':
python_exe = r'C:\Python27\python.exe'
else:
python_exe = 'python'

print '1) Initial file descriptors:\n' + fds()
f = open('fd_table_status.py', 'r')
print '2) After file open, before Popen:\n' + fds()
p = subprocess.Popen(['python', 'fd_table_status.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print '3) After Popen, before reading piped output:\n' + fds()
result = p.communicate()
print '4) After Popen.communicate():\n' + fds()
del p
print '5) After deleting reference to Popen instance:\n' + fds()
del f
print '6) After deleting reference to file instance:\n' + fds()
print '7) child process had the following file descriptors:'
print result[0][:-1]

Linux 输出

1) Initial file descriptors:
Open file handles: 0: CHR, 1: CHR, 2: CHR
2) After file open, before Popen:
Open file handles: 0: CHR, 1: CHR, 2: CHR, 3: REG
3) After Popen, before reading piped output:
Open file handles: 0: CHR, 1: CHR, 2: CHR, 3: REG, 5: FIFO, 6: FIFO, 8: FIFO
4) After Popen.communicate():
Open file handles: 0: CHR, 1: CHR, 2: CHR, 3: REG
5) After deleting reference to Popen instance:
Open file handles: 0: CHR, 1: CHR, 2: CHR, 3: REG
6) After deleting reference to file instance:
Open file handles: 0: CHR, 1: CHR, 2: CHR
7) child process had the following file descriptors:
Open file handles: 0: FIFO, 1: FIFO, 2: FIFO, 3: REG

Windows 输出

1) Initial file descriptors:
Open file handles: 0: CHR, 1: CHR, 2: CHR
2) After file open, before Popen:
Open file handles: 0: CHR, 1: CHR, 2: CHR, 3: REG
3) After Popen, before reading piped output:
Open file handles: 0: CHR, 1: CHR, 2: CHR, 3: REG, 4: FIFO, 5: FIFO, 6: FIFO
4) After Popen.communicate():
Open file handles: 0: CHR, 1: CHR, 2: CHR, 3: REG, 5: FIFO, 6: FIFO
5) After deleting reference to Popen instance:
Open file handles: 0: CHR, 1: CHR, 2: CHR, 3: REG
6) After deleting reference to file instance:
Open file handles: 0: CHR, 1: CHR, 2: CHR
7) child process had the following file descriptors:
Open file handles: 0: FIFO, 1: FIFO, 2: FIFO

如您在第 4 步中所见,Windows 的行为与 Linux 不同。必须销毁 Popen 实例才能关闭管道。

顺便说一句,第 7 步中的差异显示了有关 Windows 中 Python 解释器行为的不同问题,您可以查看有关这两个问题的更多详细信息 here .

关于Python 子进程用完文件描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6669996/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com