graw
graw

Reputation: 233

Python Game Server - Optimizing networking

I have a Python game server running. The game is turn based. Players complain about short lag spikes which are several turns in duration (ie. their client is stuck waiting for awhile and then suddenly they receive several turns worth of updates all at once). I'm hoping to find some way to improve the networking consistency, but I'm not sure what's left to be done.

Here's what I'm doing:

Here's how I receive plays:

for (fileno, event) in events:
    if fileno == self.server_socket.fileno():
        self.add_new_client()
    elif event & select.EPOLLIN:
        c = self.clients[fileno]
        c.read() # reads and processes all input and generates all output
        ...

And here's how I send updates:

if turn_finished:
    for user in self.clients.itervalues():
        for msg in user.queued_messages:
            msg = self.encode(msg)
            bytes_sent = user.socket.send(msg)
            ...

Whenever I write to or read from sockets, I check that all bytes were sent and log any socket errors. These things almost never show up in the logs.

Would it be better if I only did one socket.send() call?

Is there anything I can check or tweak on the linux (Ubuntu) host?

There seems to be some issue with data arriving late to the clients. Can anyone give any suggestions for debugging this issue?


About the messages sent:

If a player is idle, they typically receive get 0-3 updates a turn. If a player is in the middle of action with other players, they typically receive to 2-3 updates per other player on their screen. Updates are typically about <20 bytes long. There are a couple updates (only sent to 1 player at a time) that are 256 and 500-600 bytes in size.

There are typically about 10-50 players active a time, and no more than 10 in the same screen.


P.S. - I run with PyPy. I've profiled and everything looks good. All player moves are handled in <3 ms and the server idles the vast majority of the time.

Upvotes: 3

Views: 432

Answers (1)

Jean-Paul Calderone
Jean-Paul Calderone

Reputation: 48345

It sounds like you need to debug the client and find out what it is doing while it is "stuck".

Only once you understand the cause will you be able to find a solution.

Upvotes: 1

Related Questions