I plan to make a Python game server but I can't see how to set it up.
The problem I have is that I need the server to listen for connections (with socket.socket.accept()
?) but it hangs the code until a connection is made, which means the server can't do anything else while it is waiting.
Is multi-threading the solution to this?
The other question is whether each client should connect at the start of the game and stay connected for the duration, or if the connection should be made every second to send/receive data.
Correct, socket.accept()
blocks by design. To handle connection in the background see SocketServer.ThreadingMixIn
for example.
http://docs.python.org/library/socketserver.html#asynchronous-mixins
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.