Created
July 5, 2018 15:40
-
-
Save limafm/9f655a85ac57525f3bb9f2caee75a9fd to your computer and use it in GitHub Desktop.
Python multiprocessing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
import time | |
from random import randint | |
PROCESSES = 5 | |
WORKER_CALLS = 7 | |
def worker(num): | |
"""worker function""" | |
print 'Starting worker', num | |
time.sleep(randint(2,4)) | |
print 'Exiting worker', num | |
return "ok" | |
if __name__ == '__main__': | |
pool = multiprocessing.Pool(processes=PROCESSES) | |
pool_outputs = pool.map(worker, range(WORKER_CALLS)) | |
pool.close() | |
pool.join() | |
print 'Pool:', pool_outputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment