我正在使用python多处理编写测试程序。我正在使用记录器记录所取得的进展。但是记录器似乎不起作用。它不会在子进程中记录或打印。无法弄清楚出了什么问题。用普通打印语句替换记录器也无济于事。以下是我的代码:
from multiprocessing import Pool
from multiprocessing.pool import ApplyResult
import time
import os
import logging
import multiprocessing
import sys
logger=[]
class C( object ):
def __init__(self, a, b=1):
self.a = a
self.b = b
def f(self, name):
global logger
time.sleep(2)
logger.info('****Inside f..******')
return str(os.getpid())
def call_back(self, x):
print 'Inside callback', x
def _run(self):
print 'Reached inside run..'
print 'main process id ..', os.getpid()
pool = Pool(processes=6)
names = ['frank', 'justin', 'osi', 'thomas', 'jim', 'rock', 'stonecold', 'machoman']
result = pool.map_async(unwrap_self_f, zip([self]*len(names), names), 1, callback=self.call_back)
result.wait()
print type(result)
print result
def hello(self):
print 'Running hello....'
self._run()
def test():
logger.info( 'Starting....test')
c = C(1, 2)
print 'Running test....'
c.hello()
def unwrap_self_f(arg, **kwarg):
print 'inside unwrap_self_f'
return C.f(*arg, **kwarg)
if __name__ == '__main__':
print 'Loading multiprocessing...'
logger = multiprocessing.log_to_stderr()
logger.setLevel(logging.DEBUG)
test()
我得到的输出如下:
Loading multiprocessing...
[INFO/MainProcess] Starting....test
Running test....
Running hello....
Reached inside run..
main process id .. 19056
[DEBUG/MainProcess] created semlock with handle 520
[DEBUG/MainProcess] created semlock with handle 532
<class 'multiprocessing.pool.MapResult'>
<multiprocessing.pool.MapResult object at 0x030FFB30>
[DEBUG/MainProcess] finalizing pool
[DEBUG/MainProcess] helping task handler/workers to finish
[DEBUG/MainProcess] task handler got sentinel
[DEBUG/MainProcess] removing tasks from inqueue until task handler finished
[DEBUG/MainProcess] task handler sending sentinel to result handler
[DEBUG/MainProcess] task handler sending sentinel to workers
[DEBUG/MainProcess] result handler got sentinel
[DEBUG/MainProcess] task handler exiting
[DEBUG/MainProcess] terminating workers
[DEBUG/MainProcess] ensuring that outqueue is not full
[DEBUG/MainProcess] joining task handler
[DEBUG/MainProcess] result handler exiting: len(cache)=0, thread._state=2
[DEBUG/MainProcess] joining result handler
[DEBUG/MainProcess] joining pool workers
它没有记录“'****里面f .. ******'”(来自函数f) 请任何人帮我弄清楚我做错了什么。使用多处理进行记录似乎是一个棘手的问题:( 我使用的是Python 2.6.6。操作系统是windows-32bit / 64位。
答案 0 :(得分:0)
生成的进程不会初始化其日志记录。因此,您需要在每个进程(如multiprocessing.log_to_stderr
)以及主进程中的代码访问的位置调用unwrap_self_f
。例如:
def unwrap_self_f(arg, **kwarg):
global logger
print 'inside unwrap_self_f'
logger = multiprocessing.log_to_stderr()
logger.setLevel(logging.DEBUG)
return C.f(*arg, **kwarg)