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

后端 未结 2 479
一整个雨季
一整个雨季 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:33

    Ok, i somehow managed this. I looked to python documentation, and i learnt that: with using Queue class, we can get return values from a function. And final version of my code is like this:

    from multiprocessing import Process, Queue
    
    def multiply(a,b,que): #add a argument to function for assigning a queue
        que.put(a*b) #we're putting return value into queue
    
    if __name__ == '__main__':
        queue1 = Queue() #create a queue object
        p = Process(target= multiply, args= (5,4,queue1)) #we're setting 3rd argument to queue1
        p.start()
        print(queue1.get()) #and we're getting return value: 20
        p.join()
        print("ok.")
    

    And there is also a pipe() function, i think we can use pipe() function,too. But Queue worked for me, now.

    0 讨论(0)
  • 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])
    
    0 讨论(0)
提交回复
热议问题