Last active
February 16, 2021 02:50
-
-
Save cdleary/15cffcf615fe397f60ecd28fd08f59b4 to your computer and use it in GitHub Desktop.
Simple demo of processes with queues to drive control.
This file contains hidden or 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 as mp | |
def do_run(i: int, q: mp.Queue): | |
while True: | |
item = q.get() | |
if item is None: # Sentinel. | |
return | |
print(i, item) | |
COUNT = 4 | |
qs = [mp.Queue() for _ in range(COUNT)] | |
ps = [mp.Process(target=do_run, args=(i, q)) for i, q in enumerate(qs)] | |
for p in ps: p.start() | |
for i in range(12): | |
qs[i%COUNT].put(i) | |
for p, q in zip(ps, qs): | |
q.put(None) | |
p.join() | |
print('Done!') |
This file contains hidden or 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 as mp | |
def do_run(i: int, q: mp.Queue): | |
while True: | |
item = q.get() | |
if item is None: # Sentinel. | |
return | |
print(i, item) | |
COUNT = 4 | |
q = mp.Queue() | |
ps = [mp.Process(target=do_run, args=(i, q)) for i in range(COUNT)] | |
for p in ps: p.start() | |
for i in range(12): | |
q.put(i) | |
# Technically this is probably not guaranteed to terminate; e.g. one process | |
# could be fastest and starve all the others out. | |
for p in ps: | |
while p.is_alive(): | |
q.put(None) | |
p.join() | |
print('Done!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment