[英]Why won't my processes join in python?
我有一個程序,一旦程序完成就不會加入。 我怎樣才能使其正確加入?
import time
import sys
import zmq
from multiprocessing import Process, Pipe
def client(address, pipe_send):
try:
context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.setsockopt(zmq.RCVTIMEO,5000)
socket.connect(address)
print("client connected")
while True:
msg = str(pipe_send.recv())
print("CLIENT $$",msg)
if msg:
socket.send_unicode(msg)
recv_msg = socket.recv_unicode()
print("CLIENT ::", recv_msg)
else:
break
except:
print(
"client exited with '%s' while multiprocessing"
% (sys.exc_info(),)
)
def server(address):
try:
context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.setsockopt(zmq.RCVTIMEO,5000)
socket.bind(address)
while True:
try:
msg = socket.recv_unicode()
print("SERVER ::",msg)
socket.send_unicode("Message sent successfully")
time.sleep(1)
except:
break
except:
print(
"server exited with '%s' while multiprocessing"
% (sys.exc_info(),)
)
if __name__ == "__main__":
req_pipe, rep_pipe = Pipe()
addr = "tcp://127.0.0.1:1235"
s = Process(target=server,args=(addr,))
s.start()
c = Process(target=client,args=(addr,rep_pipe))
c.start()
for i in range(10):
req_pipe.send(i)
s.join()
c.join()
s = Process(target=server,args=(addr,)).start()
您已將s定義為start()
的返回值,而不是Process
本身,並且start()
的返回值必須為None
,它沒有join方法。
Proces(..).start()
分配給s
/ c
。 ''
。 if __name__ == "__main__":
req_pipe, rep_pipe = Pipe()
addr = "tcp://127.0.0.1:1235"
s = Process(target=server,args=(addr,))
s.start()
c = Process(target=client,args=(addr,rep_pipe))
c.start()
for i in range(10):
req_pipe.send(i)
req_pipe.send('') # End of data.
c.join()
s.join()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.