我的流程设置如
manager = Manager();
nodes = manager.dict();
p1 = Process(target = advertise, args=(nodes,));
p1.start();
p2 = Process(target = receive, args=(nodes,));
p2.start();
def advertise(nodes):
while(1):
nts = json.dumps(nodes.copy());
for k, v in nodes.items():
# Creates a UDP socket
mySocket = createSocket(getMyIP(), PORT);
mySocket.connect((getMyIP(), v[1]));
mySocket.send(("AD||" + nts).encode("ASCII"));
mySocket.close();
time.sleep(5);
def receive(nodes):
while(1):
mySocket = createSocket(getMyIP(), PORT);
mySocket.bind((getMyIP(), PORT));
try:
data = mySocket.recv(4096).decode("ASCII").split("||");
if(data[0] == "AD"):
temp = json.loads(data[1]);
#print("Received: " + str(temp));
for k, v in temp.items():
nodes[k] = [float(v[0]), int(v[1])];
except:
mySocket.close();
其中一个接收dict并更新值,其中一个发出一个字典。 我不能json.dumps(节点),因为它说DictProxy不是jsondumpable,所以我做了nts = json.dumps(nodes.copy());
这是第一次(套接字正确发送字典)但第二次程序只是说
File "Dvr.py", line 40, in advertise
nts = json.dumps(nodes.copy());
File "<string>", line 2, in copy
File "/usr/lib/python2.7/multiprocessing/managers.py", line 758, in _callmethod
conn.send((self._id, methodname, args, kwds))
IOError: [Errno 32] Broken pipe
1)为什么这只是第一次while循环循环?
2)我如何让它每次都有效?
答案 0 :(得分:0)
<强>固定强> 我只是使用线程而不是进程,因此更容易拥有一个全局简单字典,而不必跨多个进程的内存空间共享字典