jsonsocket icon indicating copy to clipboard operation
jsonsocket copied to clipboard

multithreading server

Open samnet opened this issue 8 years ago • 5 comments

Hello,

thanks for this pgm. I have an issue with spawning server threads using the class Server. e.g., in a case like:

while True:
    server.accept()  
    ct=sThread(server.client)
    ct.run()

I guess that optimally we would like server.client to be able to send JSON over TCP in the optimal way afforded by the class Server. But... It seems to me that server.client is, because of the definition of Server.accept(), an instance of the regular socket class. That is, it is not an instance of your improved for JSON socket class, not an instance of Server.accept(). Am I right?

Thank you

samnet avatar Jun 28 '16 17:06 samnet

You are right, server.client is a regular socket instance returned by socket.accept(). It is meant to be private, and people should only use send() and recv().

I'm not sure I completely understand your request. Do you want the JSON server to send data over your custom socket instance instead of creating its own socket?

mdebbar avatar Jun 28 '16 17:06 mdebbar

Thanks for this answer,

I understand that server.client is meant to be private in the case where we have only one thread.

But if we want the server to spawn a thread for each connection (i.e., if we have several clients and want a multithreaded server) then, what argument will we give to the threads? I see no other possibility than to give server.client as argument to the threads (as is done here for instance https://docs.python.org/2/howto/sockets.html). But then the socket object we use in each thread to communicate with the corresponding client is just a regular sockets, with no JSON optimisation.

Or is there another way to have a multithreaded server with JSON optimised sockets?

Best

samnet avatar Jun 28 '16 18:06 samnet

hmmm that's an interesting use-case. Using server.client won't help, because the server expects to have only one client at a time. If you try to accept a new connection, it will automatically close the previous one.

First, we need to change the server to have a list of clients instead of just one. Second, we need those clients to understand the JSON data.

We could create a ServerConnection class that wraps the server.client socket, and implements send() and recv() to handle JSON data. Then, we changeServer to have a list of those connections. Roughly, something like this:

class ServerConnection(object):
  ...
  def __init__(self, conn):
    self.conn = conn

  def send(self, data):
    return _recv(self.conn)
  ...

class Server(object):
  ...
  def accept_connection(self):
    conn, addr = self.socket.accept()
    self.connections.append(ServerConnection(conn))
    return self.connections[-1]
  ...

Then you could easily use it with threads:

while True:
  connection = server.accept_connection()
  ct = sThread(connection)
  ct.run()

mdebbar avatar Jun 28 '16 18:06 mdebbar

I'll be happy to review and accept a Pull Request if you have some time to create one 😊

mdebbar avatar Jun 28 '16 18:06 mdebbar

Ok, I ll start by the ServerConnection class

samnet avatar Jun 29 '16 09:06 samnet