0

我正在使用 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

它没有记录“'****Inside f..******'”(来自函数f)请任何人帮助我弄清楚我做错了什么。使用多处理进行日志记录似乎是一个难题:(我使用的是 Python 2.6.6。操作系统是 windows-32bit/64 位。

4

1 回答 1

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)    
于 2014-12-03T10:33:35.710 回答