Python concurrent.futuresマルチプロセス処理

import concurrent.futures
import logging
import time

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

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

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

if __name__ == '__main__':
main()

ThreadPoolExecutor-0_0: start
ThreadPoolExecutor-0_1: start
ThreadPoolExecutor-0_0: 10
ThreadPoolExecutor-0_1: 10
ThreadPoolExecutor-0_0: end
ThreadPoolExecutor-0_1: end
MainThread: 10
MainThread: 10

import concurrent.futures
import logging
import time

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

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

def main():
with concurrent.futures.ProcessPoolExecutor(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())

if __name__ == '__main__':
main()

SpawnProcess-1: start
SpawnProcess-2: start
SpawnProcess-1: 10
SpawnProcess-2: 10
SpawnProcess-1: end
SpawnProcess-2: end
MainProcess: 10
MainProcess: 10

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny

タイトルとURLをコピーしました