Skip to content

Instantly share code, notes, and snippets.

@m-butterfield
Last active February 27, 2016 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-butterfield/e18737ec7afe2267e22d to your computer and use it in GitHub Desktop.
Save m-butterfield/e18737ec7afe2267e22d to your computer and use it in GitHub Desktop.
"""
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