This post was originally posted to a mailing list, but it summarizes my recent thought on the question, so it is copied here.

The python sockets library is probably *not* the place to start.  It is very primitive,  and most references you will read will recommend using a  higher level library such as asyncore or (higher yet) asynchat.  Unfortunately, these higher level libraries share the same weakness as Twisted, in that that they expect to own the main loop.  I have identified five general approaches to integrating networking with a pygame application.  I have not fully implemented a game with any of them, .. but soon.

  1. Use the iterate method of the Twisted reactor.  This is the easiest approach, and the one that Mr Wittber’s library uses.  Use of reactor.iterate is unfortunately deprecated by the Twisted developers, and will, presumably, be removed from Twisted in some future release.
  2. Use the Twisted reactor *as* your main loop, and implement all game logic as call-backs from there.  This is the approach that the Twisted people will recommend.   This approach is quite burdensome, in that structuring your game into a series of callbacks can be difficult.  Using generators as coroutines provides some relief, in that you can pass the generators ‘next’ method as a callback to Twisted, and maintain game state within the generator.  Using subroutines or nested generators involve their own awkwardnesses.
  3. Run the Twisted reactor in its own thread, and use ThreadedSelectReactor to move data to the pygame event queue.  Bob Ippolito has blogged on how to use this with Pygame (GIYF).  Unfortunately, ThreadedSelectReactor is unmaintained and deprecated.
  4. Run the Twisted reactor in its own thread, and use thread-safe queues to move data to and from the main thread.  Alternatively, if you wish to use the perspective-broker features or use Twisted code beyond mere message passing, there are tools in Twisted for safely invoking methods in other threads.  Hmm. maybe these were deprecated with the TSR?
  5. Run the Twisted reactor in its own process, and use pipes or sockets to pass data between it and the main loop/process.  This has the merit of using the OS’s CPU allocation management rather than that of Python’s thread manager.

Most of these approaches could use asyncore/asynchat as an alternative to Twisted. My own efforts lately have been along approaches 2 and 5.   My engine ‘Spyre’ will run under Twisted’s reactor now, but the getting the whole multi-engine game logic sequence working in the ‘inside-out’ callback style is an imposing prospect. 

Update:
Add an option 6. Create a Twisted reactor subclass that can be exited and resumed. See later blogposts here for details.
Advertisements