Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multithreading server #5

Open
samnet opened this issue Jun 28, 2016 · 5 comments
Open

multithreading server #5

samnet opened this issue Jun 28, 2016 · 5 comments

Comments

@samnet
Copy link

samnet commented Jun 28, 2016

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

@mdebbar
Copy link
Owner

mdebbar commented Jun 28, 2016

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?

@samnet
Copy link
Author

samnet commented Jun 28, 2016

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

@mdebbar
Copy link
Owner

mdebbar commented Jun 28, 2016

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
Copy link
Owner

mdebbar commented Jun 28, 2016

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

@samnet
Copy link
Author

samnet commented Jun 29, 2016

Ok, I ll start by the ServerConnection class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants