Last active
February 27, 2016 02:37
-
-
Save m-butterfield/e18737ec7afe2267e22d to your computer and use it in GitHub Desktop.
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
""" | |
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