Multiprocessing in a pipeline done right

后端 未结 6 1204
长情又很酷
长情又很酷 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:08

    Pypeline does this for you. You can even choose between using Processes, Threads or async Tasks. What you want is just e.g. using Processes:

    import pypeln as pl
    
    data = some_iterable()
    data = pl.process.map(f2, data, workers = 3)
    data = list(data)
    

    You can do more complex stuff

    import pypeln as pl
    
    data = some_iterable()
    data = pl.process.map(f2, data, workers = 3)
    data = pl.process.filter(f3, data, workers = 1)
    data = pl.process.flat_map(f4, data, workers = 5)
    data = list(data)
    

提交回复
热议问题