[PYTHON] concurrents.futures

Si vous voulez en faire un processus, réécrivez la partie filetée à traiter.

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())
        
        #Idem pour ci-dessous
        #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

concurrents.futures