python多进程死锁_日志记录多进程/多线程python脚本的死锁

我面临从以下脚本收集日志的问题。

一旦我将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')

如何在多进程和多线程脚本中使用日志记录模块?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值