can i listen to multiple sockets at once
The code i am using to monitor the sockets at the moment is:
while True:
for sock in socks:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
but that waits at the line:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
until it recieves a message.
Is there a way to make it listen to multiple sockets at once
EDIT: not sure if it is completely relevant but i am using UDP
socket. listen(backlog) Listen for connections made to the socket. The backlog argument specifies the maximum number of queued connections and should be at least 1; the maximum value is system-dependent (usually 5). Obviously the system value is more than 5 on your system.
Listen() Function Of Socket Class In Python Calling listen() makes a socket ready for accepting connections. The listen() method should be called before calling the accept() method on the server socket. The listen() function accepts a queue size through the parameter backlog.
Python provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols.
A slight update to entropy's answer for Python 3:
The selectors
module allows high-level and efficient I/O multiplexing, built upon the select module primitives. Users are encouraged to use this module instead, unless they want precise control over the OS-level primitives used.
As per the documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With