python process return values problem

This is the place for queries that don't fit in any of the other categories.

python process return values problem

Postby ericrystal » Fri Aug 02, 2013 3:58 pm

I want to parallelize some functions, but how to get back the return values as varables to the next function as shown in the code? I have found everywhere but couldn't find, any one could help me? Thanks!

Code: Select all
from multiprocessing import Process

def func_1(value1):
    v1= value1*5
    return v1
def func_2(value2):
    v2= value2-5
    return v2
def func_3(v1,v2):
    v3=v1+v2
    return v3
if __name__ == '__main__':
   p1 = Process(target=func_1, args=(5,))
   p2 = Process(target=func_2, args=(3,))
   p1.start()
   p2.start()
   p1.join()
   p2.join()
   r=func_3(?,?)#how to get the return values of func_1 and func_2
   print r
   
ericrystal
 
Posts: 18
Joined: Thu Apr 11, 2013 8:56 am

Re: python process return values problem

Postby setrofim » Fri Aug 02, 2013 9:11 pm

Code: Select all
r = func_3(p1.exitcode, p2.exitcode)
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: python process return values problem

Postby ericrystal » Fri Aug 02, 2013 10:11 pm

setrofim wrote:
Code: Select all
r = func_3(p1.exitcode, p2.exitcode)


Thanks but I think this is a problem:

>>> p1 = Process(target=func_1, args=(5,))
p2 = Process(target=func_2, args=(3,))
p1.start()
p2.start()
p1.join()
p2.join()
r = func_3(p1.exitcode, p2.exitcode)
print r>>> >>> >>> >>> >>> >>> >>>
0
>>>
>>> p1.exitcode
0

Why p1.exitcode and p2.exitcode are 0?
ericrystal
 
Posts: 18
Joined: Thu Apr 11, 2013 8:56 am

Re: python process return values problem

Postby stranac » Fri Aug 09, 2013 4:33 pm

ericrystal wrote:Why p1.exitcode and p2.exitcode are 0?

0 return code means process completed successfully and no error occurred.

As for your actual problem, you should use a multiprocessing.Queue.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: python process return values problem

Postby ericrystal » Sat Aug 10, 2013 9:36 pm

stranac wrote:
ericrystal wrote:Why p1.exitcode and p2.exitcode are 0?

0 return code means process completed successfully and no error occurred.

As for your actual problem, you should use a multiprocessing.Queue.

Thanks, but as there is no example, it is too hard for me, so could you give me an example?
ericrystal
 
Posts: 18
Joined: Thu Apr 11, 2013 8:56 am

Re: python process return values problem

Postby stranac » Sun Aug 11, 2013 12:29 am

It's really simple - you put to the queue instead of returning, and then you get the results from the queue:
Code: Select all
import multiprocessing


def forty_two(q):
    # put to Queue instead of returning
    q.put(42)

if __name__ == '__main__':
    q = multiprocessing.Queue()

    p = multiprocessing.Process(target=forty_two, args=(q,))
    p.start()
    p.join()

    # get the result from the Queue
    print q.get()
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: python process return values problem

Postby ericrystal » Tue Aug 13, 2013 11:26 am

stranac wrote:It's really simple - you put to the queue instead of returning, and then you get the results from the queue:
Code: Select all
import multiprocessing


def forty_two(q):
    # put to Queue instead of returning
    q.put(42)

if __name__ == '__main__':
    q = multiprocessing.Queue()

    p = multiprocessing.Process(target=forty_two, args=(q,))
    p.start()
    p.join()

    # get the result from the Queue
    print q.get()


It works! Thank you very much!!
ericrystal
 
Posts: 18
Joined: Thu Apr 11, 2013 8:56 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 1 guest