[Twisted-Python] Two main loops

glyph at divmod.com glyph at divmod.com
Mon Nov 12 16:17:47 EST 2007

On 06:23 pm, jasper at peak.org wrote:
>glyph at divmod.com wrote:
>>On 09:06 pm, jasper at peak.org wrote:

>>You can't send "objects" over "Twisted".
>Sure, not per se.

>It comes in the twisted installation package, in the module 
>"twisted.spread.pb" ;-)

There are a number of other ways to communicate with other programs 
using Twisted, some of which involve "sending objects" through other 
protocols.  Obviously PB is a part of Twisted :).
>In hindsight I see a separate process won't work (duh!)  However, I was 
>thinking a thread might work, since the data I'm sending (players get a 
>filtered view of the game's true state) isn't used by anything else, 
>and the data isn't /that/ large. ;-)  Am I missing something low level 
>here?  Would this end up locking bits other than those being 
>I suppose it's a bit moot, as I'm reluctant to fiddle with threading.

If you've got a large object, let's say the state of the game world, and 
you need to lock it for use within a thread, it is likely that other 
things in your program will want to access it before long.  You can only 
have so many "large" objects in a program, after all, and they tend to 
be shared.  The one you referred to, "the game's true state", is 
obviously going to be accessed by a lot of code unless you have a very 
strange game indeed.
>Optimizing my program (e.g. by sending a bunch of smaller data chunks) 
>feels like the wrong way to go...  Wouldn't it make more sense to hack 
>PB's callRemote() to do this in a more general manner?  I'm thinking 
>somewhere around jelly._Jellier.jelly()'s recursive calls to itself; 
>perhaps using generators, although I'm fuzzy on the implications of 
>recursion + generators...

callRemote *can't* do this in a general manner and remain compatible 
with PB's existing semantics.  It would be extraordinarily difficult to 
come up with semantics that would make sense for this.  Consider:

  bob = Mob(hitPoints=3)
  jethro = Player()
  jethro.callRemote("enterSensoryRange", bob)
  bob.hitPoints += 1

How many hit points does Jethro think Bob has?  Probably 3, but if PB 
might be breaking up serialization work into separate reactor turns for 
you behind your back, then maybe 4.  Given that there's no way to know, 
you don't know whether you have to send Jethro an update to bob's hit 
points or not.

If you can figure out how to answer this question in a way that makes 
sense (i.e. never uses the word "maybe") then you might be able to 
implement something very cool and useful.  I certainly can't think of a 
way to do it, though.  Good luck!

More information about the Twisted-Python mailing list