I\'d like to know how multiprocessing is done right. Assuming I have a list [1,2,3,4,5]
generated by function f1
which is written to a Queue
With MPipe module, simply do this:
from mpipe import OrderedStage, Pipeline
def f1(value):
return value * 2
def f2(value):
print(value)
s1 = OrderedStage(f1, size=2)
s2 = OrderedStage(f2)
p = Pipeline(s1.link(s2))
for task in 1, 2, 3, 4, 5, None:
p.put(task)
The above runs 4 processes:
The MPipe cookbook offers some explanation of how processes are shut down internally using None
as the last task.
To run the code, install MPipe:
virtualenv venv
venv/bin/pip install mpipe
venv/bin/python prog.py
Output:
2
4
6
8
10