Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于登录python的父进程中存在fd泄漏_Python_Logging_Memory Leaks - Fatal编程技术网

用于登录python的父进程中存在fd泄漏

用于登录python的父进程中存在fd泄漏,python,logging,memory-leaks,Python,Logging,Memory Leaks,我有一个服务器进程,它为每个请求生成一个进程。其中父进程正在泄漏记录器的fd。请查找示例代码 from threading import Thread from multiprocessing import Process from time import sleep import logging from uuid import uuid4 class ChildFile(object): def __init__(self): self.logger

我有一个服务器进程,它为每个请求生成一个进程。其中父进程正在泄漏记录器的fd。请查找示例代码

from threading import Thread 
from multiprocessing import Process 
from time import sleep 
import logging 
from uuid import uuid4 

class ChildFile(object): 
    def __init__(self): 
        self.logger = logging.getLogger('test') 
        fh = logging.FileHandler('/tmp/test'+str(uuid4())) 
        fh.setLevel(logging.INFO) 
        self.logger.addHandler(fh) 
        self.fd = open('test2', 'wb') 

    def run(self): 
        self.logger.info('dummy run') 

def child_file_creator(): 
    a = ChildFile() 
    child_process = Process(target=a.run) 
    child_process.start() 
    child_process.join() 

if __name__ == '__main__': 
    print 'parent process run' 
    while True: 
        child_file_creator() 
        sleep(10) 
1) 子进程退出后

2) 对于父进程,fd仍然保持打开状态。 你可以使用

cd /proc/23223/fd 

Ideapad-Z570:/proc/23223/fd$ ls -ltr 
total 0 
l-wx------ 1 * * 64 Nov 11 15:10 6 -> /tmp/test62bba7f1-223c-4c17-a483-f6d92ab67222 
l-wx------ 1 * * 64 Nov 11 15:10 5 -> /tmp/test2946cdf6-7e4c-4979-b56a-fd2cc6333398 
l-wx------ 1 * * 64 Nov 11 15:10 4 -> /tmp/test0488579b-10d7-4635-abb0-a31a0ea79eeb 
lr-x------ 1 * * 64 Nov 11 15:10 3 -> /dev/urandom 
lrwx------ 1 * * 64 Nov 11 15:10 2 -> /dev/pts/19 
lrwx------ 1 * * 64 Nov 11 15:10 1 -> /dev/pts/19 
lrwx------ 1 * * 64 Nov 11 15:10 0 -> /dev/pts/19 
3) 而打开的“test2”的正常文件描述符已关闭。但连接到记录器的fd正在泄漏


如何关闭相同的记录器对象

在子进程中初始化日志记录,而不是像现在这样在父进程中初始化日志记录。那么它就不会泄露任何文件描述符。

我也有同样的想法。我在寻找是否有更干净的方法来实现这一点,即在创建对象时初始化所有内容。自年起,具有开放呼叫的fd关闭。谢谢