Multithreading to handling multiple incoming requests
<p>I am new to python. And I have to write a listener/server which will handle requests coming from same network. So I am using <code>socket</code>. I will be getting request json from client, server/listener will do processing and return the result. Thats all what is going to happen. And there will be no back and forth communication between server and client.</p>

<p>The code looks something like this:</p>

<pre><code>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    
server_address = ('localhost', 10000)            
sock.bind(server_address)    
sock.listen(1)

while True: 
    connection, client_address = sock.accept()

    while True:
        #read request data
        #process request 
        #reply response to **connection** object
        break  #continue waiting for new request
</code></pre>

<p>Now the issue is that until the current request's processing is done, server will not accept new request. I somehow have to perform processing and sending response reply in separate concurrent thread. I tried by encapsulating the processing logic in separate class <code>RequestHandler</code> extending the <code>Thread</code> class. I passed <code>connection</code> object to the instance of my custom class <code>RequestHandler</code>. 
I did something like this:</p>

<pre><code>requestHandler = RequestHandler(deepcopy(connection))
requestHandler.daemon = True
requestHandler.start()  
</code></pre>

<p>However here is where I have stuck. Can I pass same <code>connection</code> object to new instances <code>RequestHandler</code> spawned each time I receive new request? Deep copying as in above code gave me following error:</p>

<pre><code>TypeError: Cannot serialize socket object
</code></pre>

<p>I am doing something absolutely wrong? Should I follow some different approach? </p>

<p>No need to extend <code>Thread</code> class. The simpliest version is this:</p>

<pre><code>import threading

def client_handler(connection):
    #read request data
    #process request 
    #reply response to **connection** object

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    
server_address = ('localhost', 10000)            
sock.bind(server_address)    
sock.listen(1)

while True: 
    connection, client_address = sock.accept()
    threading.Thread(target=client_handler, args=(connection,)).start()
</code></pre>

<p>Now you may wrap your socket object into <code>RequestHandler</code> class if you wish, that's fine. But you can't copy sockets. Just pass a reference instead: <code>RequestHandler(connection)</code>. Why would you copy it anyway?</p>

<p>The reason it is not copyable is because it represents some system resource (i.e. the socket). If there were 2 objects pointing at the same resource then they would conflict with each other. And at C level it is extremely hard to synchronize pure file descriptors (sockets are simply numbers at C level) if there are multiple owners. So socket object is a sort of a unique "gateway" to the underlying file descriptor.</p>

python multithreading sockets