Logger 不适用于 python 多处理



我正在使用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。操作系统是窗口-32位/64位。

生成的进程不会初始化其日志记录。因此,您需要在每个进程(如unwrap_self_f)以及主进程中的代码将访问的位置调用multiprocessing.log_to_stderr。例如:

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)    

相关内容

  • 没有找到相关文章

最新更新