0
我在寫一個測試程序,使用python multiprocessing。我正在使用記錄器記錄所取得的進展。然而,記錄器似乎不起作用。它不會從子進程內記錄或打印。不清楚發生了什麼問題。用普通打印語句替換記錄器也無濟於事。下面是我的代碼:Logger does not work with python multiprocessing
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 OS是Windows的32位/ 64位。