How do I parallelize a simple Python loop?

后端 未结 13 1301
北荒
北荒 2020-11-22 11:54

This is probably a trivial question, but how do I parallelize the following loop in python?

# setup output lists
output1 = list()
output2 = list()
output3 =          


        
相关标签:
13条回答
  • 2020-11-22 12:37

    Using multiple threads on CPython won't give you better performance for pure-Python code due to the global interpreter lock (GIL). I suggest using the multiprocessing module instead:

    pool = multiprocessing.Pool(4)
    out1, out2, out3 = zip(*pool.map(calc_stuff, range(0, 10 * offset, offset)))
    

    Note that this won't work in the interactive interpreter.

    To avoid the usual FUD around the GIL: There wouldn't be any advantage to using threads for this example anyway. You want to use processes here, not threads, because they avoid a whole bunch of problems.

    0 讨论(0)
提交回复
热议问题