Correct multiprocessing for handling UDP in Python

I am trying to implement a simple client and UDP server. The server should receive the message and return the converted one.

My main technology for the server is listening for UDP messages in a loop, then enter multiprocessing.Processfor each incoming message and send a response in each instance Process:

class InputProcessor(Process):
    ...

    def run(self):
        output = self.process_input()
        self.sock.sendto(output, self.addr) # send a reply

if __name__ == "__main__":
    print "serving at %s:%s" % (UDP_IP, UDP_PORT)

    sock = socket.socket(socket.AF_INET,    # Internet
                         socket.SOCK_DGRAM) # UDP
    sock.bind((UDP_IP,UDP_PORT))

    while True:
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print "received message: %s from %s:%s" % (data, addr[0], addr[1])
        p = InputProcessor(sock, data, addr)
        p.start()

In the test client, I am doing something like this:

def send_message(ip, port, data):
    sock = socket.socket(socket.AF_INET,    # Internet
                         socket.SOCK_DGRAM) # UDP
    print "sending: %s" % data
    sock.sendto(data, (ip, port))
    sock.close()


for i in xrange(SECONDS*REQUESTS_PER_SECOND):
    data = generate_data()
    p = multiprocessing.Process(target=send_message, args=(UDP_IP,
                                                           UDP_PORT,
                                                           data))
    p.start()
    time.sleep(1/REQUESTS_PER_SECOND)

The problem that I encountered is that when REQUESTS_PER_SECONDit gets above a certain value (~ 50), it seems that some client processes receive answers intended for different processes, that is, process C # 1 receives an answer for process No. 2, and vice versa .

, , - , - . , - Twisted, hovewer, . .

+3
2

, ! , , . multiprocessing.Manager().dict() , . dict(), , .

, , .

+1

, , , UDP . , , -, , . , , , , 50 / -, . , , UDP, , , , . , .

+2

All Articles