0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

プール

Last updated at Posted at 2020-04-23
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())
ForkPoolWorker-1: start
ForkPoolWorker-1: end
MainProcess: 200
MainProcess: excuted apply
MainProcess: excuted
ForkPoolWorker-2: start
ForkPoolWorker-3: start
ForkPoolWorker-2: end
ForkPoolWorker-3: end
MainProcess: 100
MainProcess: 100
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?