Writing a small battleship game server in Python

Paul Rubin http
Fri Aug 12 21:00:51 EDT 2005


Michael Goettsche <mail at tuxipuxi.org> writes:
> Assuming the server accepts connections in an endless loop, how
> would I handle communication without additional threads and create
> new games in that loop?  Could you give me pseudo-code for this?

I've always done this kind of thing with the SocketServer module
and the threading mixin.  You'd say something like:

    from SocketServer import SocketServer, ThreadingMixin

    class Battleship_server(ThreadingMixin, SocketServer):
      def handle(self, request):
        # this method gets called in a new thread when a new connection
        # arrives.  Just handle the whole game for that connection here.

You do need some synchronization to avoid concurrency hazards when
the different threads touch shared data (like a global scoreboard)
but it's not as bad as it sounds.



More information about the Python-list mailing list