Assigning return value of function to a variable, with multiprocessing? And a problem about IDLE?

后端 未结 2 480
一整个雨季
一整个雨季 2020-12-14 03:45

I\'m trying to understand multiprocessing in python.

from multiprocessing import Process

def multiply(a,b):
    print(a*b)
    return a*b

if __name__ == \'         


        
2条回答
  •  星月不相逢
    2020-12-14 04:37

    Does this help? This takes a list of functions (and their arguments), runs them in parallel, and returns their outputs.: (This is old. Much newer version of this is at https://gitlab.com/cpbl/cpblUtilities/blob/master/parallel.py )

    def  runFunctionsInParallel(listOf_FuncAndArgLists):
        """
        Take a list of lists like [function, arg1, arg2, ...]. Run those functions in parallel, wait for them all to finish, and return the list of their return values, in order.
    
    (This still needs error handling ie to ensure everything returned okay.)
    
        """
        from multiprocessing import Process, Queue
    
        def storeOutputFFF(fff,theArgs,que): #add a argument to function for assigning a queue
            print 'MULTIPROCESSING: Launching %s in parallel '%fff.func_name
            que.put(fff(*theArgs)) #we're putting return value into queue
    
        queues=[Queue() for fff in listOf_FuncAndArgLists] #create a queue object for each function
        jobs = [Process(target=storeOutputFFF,args=[funcArgs[0],funcArgs[1:],queues[iii]]) for iii,funcArgs in enumerate(listOf_FuncAndArgLists)]
        for job in jobs: job.start() # Launch them all
        for job in jobs: job.join() # Wait for them all to finish
        # And now, collect all the outputs:
        return([queue.get() for queue in queues])
    

提交回复
热议问题