import logging
import multiprocessing
import time
logging.basicConfig(level=logging.DEBUG, format='%(processName)s: %(message)s')
def worker1(i):
logging.debug('start')
time.sleep(5)
logging.debug('end')
return i
if __name__ == '__main__':
with multiprocessing.Pool(3) as p: #3個まで同時にプロセスを実行できる
logging.debug(p.apply(worker1, (200,))) #値が返ってくるまで次に進まない
logging.debug('excuted apply')
p1 = p.apply_async(worker1, (100,)) #非同期なので値が返ってくるのを待たずに次に進む
p2 = p.apply_async(worker1, (100,))
logging.debug('excuted')
logging.debug(p1.get())
logging.debug(p2.get())