I have processes set up like
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();
One of which receives a dict and updates values, one of which sends out a dict. I can't json.dumps(nodes) because it says DictProxy is not jsondumpable, so I did nts = json.dumps(nodes.copy());
This works for the first time (the socket sends the dictionary correctly) but the second time the program just says
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) Why does this only work for the first time the while loop loops?
2) How do I make it work everytime?
固定我只是使用线程而不是进程,因此拥有全局简单字典而不是必须跨多个进程的内存空间共享字典会更容易
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.