0
0

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.

マップ

Posted at
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 * 2


if __name__ == '__main__':
    with multiprocessing.Pool(3) as p:
        r = p.map(worker1, [100, 200])
        logging.debug('excuted map')
        logging.debug(r)

        r = p.map_async(worker1, [100, 200])
        logging.debug('excuted map_async')
        logging.debug(r.get())

        r = p.imap(worker1, [100, 200])
        logging.debug('excuted imap')
        logging.debug([i for i in r])

        #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-2: start
ForkPoolWorker-1: end
ForkPoolWorker-2: end
MainProcess: excuted map
MainProcess: [200, 400]
MainProcess: excuted map_async
ForkPoolWorker-1: start
ForkPoolWorker-3: start
ForkPoolWorker-1: end
ForkPoolWorker-3: end
MainProcess: [200, 400]
MainProcess: excuted imap
ForkPoolWorker-2: start
ForkPoolWorker-1: start
ForkPoolWorker-2: end
ForkPoolWorker-1: end
MainProcess: [200, 400]
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?