How do I parallelize a simple Python loop?

后端 未结 13 1524
北荒
北荒 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:20

    why dont you use threads, and one mutex to protect one global list?

    import os
    import re
    import time
    import sys
    import thread
    
    from threading import Thread
    
    class thread_it(Thread):
        def __init__ (self,param):
            Thread.__init__(self)
            self.param = param
        def run(self):
            mutex.acquire()
            output.append(calc_stuff(self.param))
            mutex.release()   
    
    
    threads = []
    output = []
    mutex = thread.allocate_lock()
    
    for j in range(0, 10):
        current = thread_it(j * offset)
        threads.append(current)
        current.start()
    
    for t in threads:
        t.join()
    
    #here you have output list filled with data
    

    keep in mind, you will be as fast as your slowest thread

提交回复
热议问题