你好,下面是一个用process带返回值的例子。主要是需要用pipe来做 from multiprocessing import Process, Pipe SENTINEL = 'SENTINEL' def sim_busy(write_conn, x): for _ in range(int(x)): assert 1 == 1 result = x write_conn.send(result) # If all results are send, send a sentinel-value to let the parent know # no more results will come. write_conn.send(SENTINEL) if __name__ == '__main__': # duplex=False because we just need one-way communication in this case. read_conn, write_conn = Pipe(duplex=False) p = Process(target=sim_busy, args=(write_conn, 150e6)) # 150e6 == 150000000.0 p.start() for result in iter(read_conn.recv, SENTINEL): # sentinel breaks the loop print(result)
你好,下面是一个用process带返回值的例子。主要是需要用pipe来做
from multiprocessing import Process, Pipe
SENTINEL = 'SENTINEL'
def sim_busy(write_conn, x):
for _ in range(int(x)):
assert 1 == 1
result = x
write_conn.send(result)
# If all results are send, send a sentinel-value to let the parent know
# no more results will come.
write_conn.send(SENTINEL)
if __name__ == '__main__':
# duplex=False because we just need one-way communication in this case.
read_conn, write_conn = Pipe(duplex=False)
p = Process(target=sim_busy, args=(write_conn, 150e6)) # 150e6 == 150000000.0
p.start()
for result in iter(read_conn.recv, SENTINEL): # sentinel breaks the loop
print(result)
一周热门 更多>