[PYTHON] concurrent.futures

If you want to make it a process, rewrite the thread part to process.

import concurrent.futures
import logging
import time

logging.basicConfig(level=logging.DEBUG, format='%(threadName)s: %(message)s')

def worker(x, y):
    logging.debug('start')
    r = x * y
    logging.debug(r)
    logging.debug('end')
    return r

def main():
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        f1 = executor.submit(worker, 2, 5)
        f2 = executor.submit(worker, 2, 5)
        logging.debug(f1.result())
        logging.debug(f2.result())
        
        #Same for below
        #args = [[2, 2], [5, 5]]
        #r = executor.map(worker, *args)
        #logging.debug(r)
        #logging.debug([i for i in r])

if __name__ == '__main__':
    main()
ThreadPoolExecutor-0_0: start
ThreadPoolExecutor-0_0: 10
ThreadPoolExecutor-0_0: end
ThreadPoolExecutor-0_0: start
ThreadPoolExecutor-0_0: 10
ThreadPoolExecutor-0_0: end
MainThread: 10
MainThread: 10

Recommended Posts

concurrent.futures