-
Notifications
You must be signed in to change notification settings - Fork 34
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
Comments
You are right, 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? |
Thanks for this answer, I understand that server.client is meant to be private in the case where we But if we want the server to spawn a thread for each Or is there another way to have a multithreaded server with JSON optimised Best |
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 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() |
I'll be happy to review and accept a Pull Request if you have some time to create one 😊 |
Ok, I ll start by the ServerConnection class |
Hello,
thanks for this pgm.
I have an issue with spawning server threads using the class Server. e.g., in a case like:
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
The text was updated successfully, but these errors were encountered: