Skip to content

Instantly share code, notes, and snippets.

@ourway
Forked from axolx/gist:3068001
Created May 23, 2020 02:47
Show Gist options
  • Save ourway/889e77f506aaa3f4b55c88a5a1170e94 to your computer and use it in GitHub Desktop.
Save ourway/889e77f506aaa3f4b55c88a5a1170e94 to your computer and use it in GitHub Desktop.
Python multiprocessing example: run a function 7 times on a 5 process pool
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