[Twisted-Python] Other ways to integrate foreign event loops?

Drew Smathers drew.smathers at gmail.com
Tue Apr 1 00:18:57 EDT 2008


On Mon, Mar 31, 2008 at 10:17 PM,  <glyph at divmod.com> wrote:
>
>  On 31 Mar, 10:45 pm, nathan.stocks at gmail.com wrote:
>  >Is there a way to do the opposite of task.coiterate?  I'm using pyglet
>  >for my UI, which was working great until I upgraded from pyglet 1.0 to
>  >1.1.  Now for some reason, the pyglet performance tanked.
>
>  Twisted's job is to run the main loop; it's best just to let it do that.
>  If Pyglet slowed down, it would be best to examine what's gone wrong
>  with Pyglet and simply submit a fix for that.
>

Yeah, I've experienced some performance issues with pyglet's
app.runtime (maybe Linux-specific or something).  Given the time, I'll
submit a meaningful bug report concerning this.  Regarding integration
with Twisted, though, I think the best thing to do (for now) is to
just ignore pyglet.app.runtime and write a pump function, roughly:

def pump():
    dt = clock.tick(True) # polling tick
    w.clear()
    w.dispatch_events()
    ...
    w.flip()
    if w.has_exit and reactor.running:
        reactor.stop()

Be sure to call clock.tick(True) - since clock.tick will otherwise
block.  The next thing to do is customize clock where appropriate -
that is, to used Twisted's scheduling functions.  Coincidentally, I
did this about 45 minutes ago for a project I'm working on:

http://hg.enterthefoo.com/roboto/file/db931f29b98e/roboto/clock.py

Beware - the above is half-baked and written under the influence of
Hobgoblin Dark English Ale.

Finally, you need schedule the pump function.  To do it as fast as possible:

    from twisted.internet import task
    gameLoop = task.LoopingCall(pump)
    gameLoop.start(0., True)
    reactor.run()

Or with a set framerate:
   ...
   gameLoop.start(1/60., True)

Cheers,

-- 
\\\\\/\"/\\\\\\\\\\\
\\\\/ // //\/\\\\\\\
\\\/ \\// /\ \/\\\\
\\/ /\/ / /\/ /\ \\\
\/ / /\/ /\ /\\\ \\
/ /\\\ /\\\ \\\\\/\
\/\\\\\/\\\\\/\\\\\\
 d.p.s




More information about the Twisted-Python mailing list