LoginSignup
0
1

More than 3 years have passed since last update.

concurrent.futures

Last updated at Posted at 2020-04-24

processにする場合は、threadの部分を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())

        #下記でも同じ
        #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
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1