Learning Python Network Programming

(Sean Pound) #1
Chapter 8
while True:
client_sock,addr = listen_sock.accept()
q = queue.Queue()
with lock:
send_queues[client_sock.fileno()] = q
recv_thread = threading.Thread(target=handle_client_recv,
args=[client_sock, addr],
daemon=True)
send_thread = threading.Thread(target=handle_client_send,
args=[client_sock, q,
addr],
daemon=True)
recv_thread.start()
send_thread.start()
print('Connection from {}'.format(addr))

We're now using two threads per client. One thread handles the messages received
and the other thread handles the task of sending messages. The idea here is to
break out each place a block might happen into its own thread. This will give us
the lowest latency for each client, but it does come at the cost of system resources.
We're reducing the potential number of clients that we may be able to handle
simultaneously. There are other models that we could use, such as having a single
thread for each client which receives messages and then sends them itself to all the
connected clients, but I've chosen to optimize for latency.


To facilitate the separate threads, we've broken the receiving code and the sending
code into the handle_client_recv() function and handle_client_send()
function respectively.


Our handle_client_recv threads are tasked with receiving messages from the
clients, and our handle_client_send threads are tasked with sending messages
to the clients, but how do the received messages get from the receive threads to the
send threads? This is where the queue, send_queue, dict and lock objects come in.


Queues


A Queue is a first-in first-out (FIFO) pipe. You add items to it by using the put()
method, and pull them out by using the get() method. The important thing about
Queue objects is that they are completely thread safe. Objects in Python are generally
not thread safe unless it is explicitly specified in their documentation. Being thread
safe means that operations on the object are guaranteed to be atomic, that is, they
will always complete without any chance of another thread getting to that object and
doing something unexpected to it.

Free download pdf