Multiprocessing in a pipeline done right

后端 未结 6 1195
长情又很酷
长情又很酷 2021-02-04 05:06

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

6条回答
  •  孤街浪徒
    2021-02-04 06:06

    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:

    • two for the first stage (function f1)
    • one for the second stage (function f2)
    • and one more for the main program that feeds the pipeline.

    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
    

提交回复
热议问题