Reputation: 107
I am setting up a pretty simple message server using TCP and UDP on different ports on a Raspberry PI 2 with wifi. I am using UDP for multi-cast. The server works fine. The problem is that the processor jumps to about 30% processor usage when I run the server (TCP and UDP listeners). This isn't processing anything, just listening. When I shut down the UDP it goes to about 3-5%. Is this normal (it is a RPi), or am I doing something wrong? Here is the code for initializing the UDP listener:
class UDPServer(asyncore.dispatcher):
def __init__(self):
asyncore.dispatcher.__init__(self)
multicast_group = '224.3.29.71'
server_address = ('', 8081)
# Create the socket
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind to the server address
self.bind(server_address)
group = socket.inet_aton(multicast_group)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
self.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
print("UDP listening on {}:{}".format(multicast_group,8081))
Upvotes: 2
Views: 1356
Reputation: 107
It took me a while to get back to this, but J.F. Sebastian has the solution. I implemented asyncio
and it works with very little overhead. I straight copied the demo from Python documentation. The only issue with asyncio is that it appears to be listed as provisional so technically it could be removed later. If you want credit for an answer move your comment to an answer and I will mark it. Thanks J. F. Sebastion and dsgdfg.
Upvotes: 1