Skip to content

Instantly share code, notes, and snippets.

@m-butterfield
Last active Feb 27, 2016
Embed
What would you like to do?
"""
Run N number of processes at once
"""
from itertools import izip_longest
from multiprocessing import Process
import time
DATA = (('a', '2'), ('b', '4'), ('c', '6'), ('d', '8'),
('e', '1'), ('f', '3'), ('g', '5'), ('h', '7'))
NUM_SIMULTANEOUS_PROCESSES = 3
def mp_handler():
for data in izip_longest(*[iter(DATA)] * NUM_SIMULTANEOUS_PROCESSES):
_handle(filter(lambda x: x is not None, data))
def _handle(data):
procs = []
num_procs = 0
for args in data:
proc = Process(target=mp_worker, args=args)
proc.start()
num_procs += 1
procs.append(proc)
for proc in procs:
proc.join()
def mp_worker(proc_name, seconds_to_wait):
print " Processs {}\tWaiting {} seconds".format(proc_name, seconds_to_wait)
time.sleep(int(seconds_to_wait))
print " Process {}\tDONE".format(proc_name)
if __name__ == '__main__':
mp_handler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment