alphanumeric
alphanumeric

Reputation: 19379

How to return value from function run by multiprocessing process

The variable time_completed gets None while the goal is to get time at which the process function was completed. How to make sure the time is returned back to the parent?

import time, multiprocessing

def process():
    num = int()
    while True:
        print '...sleeping %s' % num
        time.sleep(1)
        num += 1
        if num > 10:
            break
    return time.time() 


class Child(object):
    def __init__(self):
        super(Child, self).__init__()

    def process(self):
        proc = multiprocessing.Process(target=process)
        proc.start()

class Parent(object):
    def __init__(self):
        super(Parent, self).__init__()
        child = Child()
        time_completed = child.process()
        print 'time_completed: %s' % time_completed



obj = Parent()

Upvotes: 1

Views: 3514

Answers (2)

Mike McKerns
Mike McKerns

Reputation: 35247

You can use a Pipe, or a shared memory Value (or similarly an Array), to communicate between processes. Here's an example of using a Pipe:

import multiprocessing as mp

def worker(p):
    msg = 'Hello from child!'
    print("sending {!r} to parent".format(msg))
    p.send(msg)
    v = p.recv()
    print("got {!r} from parent".format(v))

if __name__ == '__main__':
    p_conn, c_conn = mp.Pipe()
    p = mp.Process(target=worker, args=(c_conn,))
    p.start()
    msg = 'Hello from parent!'
    print("got {!r} from child".format(p_conn.recv()))
    print("sending {!r} to child".format(msg))
    p_conn.send(msg)
    p.join()

Or, you could use a Pool, which works in the most general case of needing N embarrassingly parallel workers, each with a return value. (Note, I'm using multiprocess here, which is a bit more flexible than multiprocessing -- e.g. it works better in the interpreter):

>>> import multiprocess as mp
>>> import time
>>> def process(n):
...     num = int()
...     while True:
...         print '...sleeping %s' % num
...         time.sleep(1)
...         num += 1
...         if num > 10:
...             break
...     return time.time() 
... 
>>> mp.Pool(2).map(process, [None]*2)
...sleeping 0
...sleeping 0
...sleeping 1
...sleeping 1
...sleeping 2
...sleeping 2
...sleeping 3
...sleeping 3
...sleeping 4
...sleeping 4
...sleeping 5
...sleeping 5
...sleeping 6
...sleeping 6
...sleeping 7
...sleeping 7
...sleeping 8
...sleeping 8
...sleeping 9
...sleeping 9
...sleeping 10
...sleeping 10
[1540486371.700522, 1540486371.700522]

Upvotes: 1

FFF
FFF

Reputation: 274

First, you need to wait for your processes. For example by calling join.

Second, process() should store a value in Child that can be access afterwards and returned.

Upvotes: 0

Related Questions