ProcessPoolExecutor log error?

I am creating a multiprocessor program for processing several batches, but my log cannot write a packet to a log file, only root log.info will be written, how can I configure logging to print correctly to a log file?

The line will be printed in the log "INFO: root: this is the root log"

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()

works on windows / python2.7

+4
source share
1 answer

. , , , 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()
+1

Source: https://habr.com/ru/post/1677051/


All Articles