[Twisted-Python] Two main loops

Nitro nitro at dr-code.org
Sun Nov 11 09:20:46 EST 2007

Am 11.11.2007, 15:03 Uhr, schrieb Bernie Roehl <broehl at gmail.com>:

> I've got a 3D game that I've implemented in Python using an existing
> game engine.  The engine "owns" the main loop, i.e. I call
> engine.run() and it makes callbacks to my application on every frame
> (and other game event).
> Now I'm adding networking, and I'd like to use Twisted.  However,
> Twisted uses the same approach as the game engine -- I call
> reactor.run() and it makes callbacks to my application.
> So, how do I integrate the two?
> My first though is to call reactor.run() in a separate thread, but of
> course that adds additional complexity in terms of synchronization.
> I notice the docs mention reactor.iterate(), which appears to be
> designed for exactly what I'm doing.  However, it's not clear how to
> use it, and the method appears to be deprecated.
> Any suggestions are appreciated...

I am using twisted with our home-brewn 3d engine and I do it like this:

def DoOneLoopIteration():
     reactor.callLater(0, DoOneLoopIteration)

reactor.callLater(0, DoOneLoopIteration)

There are other possibilities like

import twisted.internet.task
scheduleUpdate = twisted.internet.task.LoopingCall(DoOneLoopIteration)
scheduleUpdate.start(0.01, False)   # 100 Hz

They all have in common that twisted is driving the main loop and the 3d  
part of the app only executes the inner part of it's main loop. I tried  
the opposite way before (3d part of the app driving main loop, twisted  
being run with iterate()) but this wasn't really usable.


More information about the Twisted-Python mailing list