Q:

python socket listener high load

I have a threaded Python listener designed to read, validate and write the data into files.
So far I have it limited to handle only 20 concurrent connections but now I need it to handle more, hundreds.. maybe 1000.
The exchange of data is a series of read writes between the client and server:
hello, username, password, category, multi-line text, bye
There is a 1 second sleep between each operation.
This is an example code to show the way it was designed.
  def handler(csock, caddr):
    while 1:
      # read hello
      # send welcome

      # read username
      # send true/false

      # read password
      # send true/false

      # read payload
      # send true/false

      # read bye
      # send bye

      # close connection
      # write data to file

      # Example how read and send are done
      inc = csock.recv(1024)
      csock.send(out)

  serversocket = socket(AF_INET, SOCK_STREAM)
  serversocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  serversocket.bind(('', 50005))
  serversocket.listen(20)

  while 1:
    csock, caddr = serversocket.accept()
    threading.Thread(target=handler, args=(csock, caddr,)).start()

My questions:
Is Python capable to operate without problems with up to 1000 concurrent connections?
If my code design solid enough for this?

A:

Python threads are locked by something called the Global Interpreter Lock. What this means is that any threads spawned by a python interpreter are all still run under one main thread. They cannot be multithreaded (at least not in vanilla python). So in your case, while you are spawning a thread per connection, they are still running sequentially.
One workaround is the multiprocessing module of python.
Python also has some default modules to help accomplish your task.
^^^These map directly to your idea of assigning a thread/process to each client.
Also consider looking at twisted, a 3rd party python library.
If on linux ( I think it maps to windows/mac as well), consider looking up the select statement in python. It (basically) checks a large list of connections and returns the ones with waiting input/output.

