我在程序中遇到错误,该程序应该运行很长时间才能打开太多文件。有什么方法可以跟踪哪些文件是打开的,所以我可以偶尔打印出这个列表,看看问题出在哪里?
发布于 2018-09-11 15:21:16
虽然上面打开的解决方案对于自己的代码很有用,但我正在调试我的客户端到第三方库,包括一些c扩展代码,所以我需要一个更直接的方法。以下例程在darwin下工作,并且(我希望)其他类似unix的环境:
def get_open_fds():
'''
return the number of open file descriptors for current process
.. warning: will only work on UNIX-like os-es.
'''
import subprocess
import os
pid = os.getpid()
procs = subprocess.check_output(
[ "lsof", '-w', '-Ff', "-p", str( pid ) ] )
nprocs = len(
filter(
lambda s: s and s[ 0 ] == 'f' and s[1: ].isdigit(),
procs.split( '\n' ) )
)
return nprocs
如果有人可以扩展到可移植到Windows,我将不胜感激。
发布于 2018-09-11 15:58:17
在Linux上,您可以使用lsof
显示进程打开的所有文件。
https://stackoverflow.com/questions/-100002610
复制相似问题