我正在创建一个多处理程序来处理多个批处理,但是我的日志记录无法将批处理记录到日志文件中,只会记录root log.info,如何设置日志记录以正确打印到日志文件?
日志只会打印这样一行“INFO:root:这是root日志记录 “
import logging
import concurrent.futures
def process_batchs():
batches = [i for i in range(100)]
logging.basicConfig(filename=r'doc\test_ProcessPoolExecutor.log', filemode='w+',level=logging.DEBUG)
logging.info('this is root logging')
with concurrent.futures.ProcessPoolExecutor(10) as e:
futures = []
for batch in batches:
future = e.submit(f, batch)
futures.append(future)
while True:
dones = [future.done() for future in futures]
if all(dones):
results = [future.result() for future in futures]
print results
break
def f(batch):
# do some thing
logging.info('this is sub logging' + str(batch))
return batch
if __name__ == '__main__':
process_batchs()
在windows / python2.7上运行
答案 0 :(得分:2)
日志记录在每个子进程中使用不同的实例,并且无法写入同一文件。应用以下修复将解决问题,但我认为通过使用logging.getlogger(' abc'),更好的解决方案可能是单例模式?
import logging
import concurrent.futures
def process_batchs():
batches = [i for i in range(100)]
logging.basicConfig(filename=r'test_ProcessPoolExecutor.log', filemode='w+',level=logging.DEBUG)
logging.info('this is root logging')
with concurrent.futures.ProcessPoolExecutor(10) as e:
futures = []
for batch in batches:
future = e.submit(f, batch)
futures.append(future)
while True:
dones = [future.done() for future in futures]
if all(dones):
results = [future.result() for future in futures]
print results
break
def f(batch):
# do some thing
# Here is the trick, notice here!!!
########
logging.basicConfig(filename=r'test_ProcessPoolExecutor.log', filemode='w+',level=logging.DEBUG)
########
logging.info('this is sub logging' + str(batch))
return batch
if __name__ == '__main__':
process_batchs()