如何通过多处理过程 return 函数 运行 的值
How to return value from function run by multiprocessing process
变量time_completed
得到None
,而目标是得到process
函数完成的时间。如何确保时间返回到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()
首先,您需要等待进程。
例如通过调用 join.
其次,process() 应该在 Child 中存储一个值,之后可以访问并返回该值。
您可以使用 Pipe
或共享内存 Value
(或类似的 Array
)在进程之间进行通信。这是使用 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()
或者,您可以使用 Pool
,它适用于最常见的情况,即需要 N
个令人尴尬的并行工作者,每个工作者都有一个 return 值。 (注意,我在这里使用 multiprocess
,它比 multiprocessing
更灵活一点——例如,它在解释器中工作得更好):
>>> 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]
变量time_completed
得到None
,而目标是得到process
函数完成的时间。如何确保时间返回到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()
首先,您需要等待进程。 例如通过调用 join.
其次,process() 应该在 Child 中存储一个值,之后可以访问并返回该值。
您可以使用 Pipe
或共享内存 Value
(或类似的 Array
)在进程之间进行通信。这是使用 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()
或者,您可以使用 Pool
,它适用于最常见的情况,即需要 N
个令人尴尬的并行工作者,每个工作者都有一个 return 值。 (注意,我在这里使用 multiprocess
,它比 multiprocessing
更灵活一点——例如,它在解释器中工作得更好):
>>> 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]