リクエストごとにプロセスを生成するサーバープロセスがあります。親プロセスがロガーの 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 がリークしています。
ロガーオブジェクトと同じように閉じるにはどうすればよいですか。