【发布时间】:2014-07-01 11:54:07
【问题描述】:
我遇到了从以下脚本收集日志的问题。
一旦我将SLEEP_TIME 设置为太“小”的值,LoggingThread
线程以某种方式阻塞了日志记录模块。记录请求时脚本冻结
在action 函数中。如果SLEEP_TIME 约为 0.1,则脚本收集
所有日志消息都符合我的预期。
我尝试关注this answer,但它并没有解决我的问题。
import multiprocessing
import threading
import logging
import time
SLEEP_TIME = 0.000001
logger = logging.getLogger()
ch = logging.StreamHandler()
ch.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(): %(message)s'))
ch.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
logger.addHandler(ch)
class LoggingThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
logger.debug('LoggingThread: {}'.format(self))
time.sleep(SLEEP_TIME)
def action(i):
logger.debug('action: {}'.format(i))
def do_parallel_job():
processes = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=processes)
for i in range(20):
pool.apply_async(action, args=(i,))
pool.close()
pool.join()
if __name__ == '__main__':
logger.debug('START')
#
# multithread part
#
for _ in range(10):
lt = LoggingThread()
lt.setDaemon(True)
lt.start()
#
# multiprocess part
#
do_parallel_job()
logger.debug('FINISH')
如何在多进程和多线程脚本中使用日志模块?
【问题讨论】:
-
我似乎无法重现您的问题。能否提供线程创建/启动代码?
-
只是为了确定:action() 的执行冻结(永远不会产生带有“actions:”的日志消息)。 LoggingThreads 永远做他们的工作。
-
死锁的概率取决于
SLEEP_TIME的值。
标签: python multithreading logging multiprocess