As far as foreign event loop integration is concergned, I think what's
needed is much simpler than this. Extract the inner parts of pyglet's
while loop into a function (pump or whatever) than can be called by
another event loop for each iteration. The next trick is dealing with
clock - which is alo somewhat simple since you can override pyglet's
default with your own - it would be nice of course to have a a few
custom ones builtin (batteries included). If you were using Twisted,
for example, you would want to use the scheduling provided by twisted
or ... bad things would likely happen.
Beyond tweaking pyglet.app as above, one problem I've spotted (in the
case of twisted) is pyglet's BaseEventLoop idle method which has a
blocking call:
dt = clock.tick(True)
This would necessitate that you use _threadedselect which really isn't
ideal. One way to deal with this maybe to add callable argument to
tick which custom clock implmentations could deal with. So idle would
become:
def idle(self):
clock.tick(True, callback=self._redraw)
def _redraw(self, dt):
# Redraw all windows
for window in windows:
window.switch_to()
window.dispatch_event('on_draw')
window.flip()
# Update timeout
return clock.get_sleep_time(True)
Richard - if you're reading this - what kind of insights did you get
from the twisted developers at pycon regarding possible integration
with pyglet?
> I'm beginning to think there is no way to do what I want since python
> doesn't allow for symmetric coroutines. Am I missing something?
Horray! Someone finally admits python doesn't have `true' (or
symmetric) coroutines.
--
\\\\\/\"/\\\\\\\\\\\
\\\\/ // //\/\\\\\\\
\\\/ \\// /\ \/\\\\
\\/ /\/ / /\/ /\ \\\
\/ / /\/ /\ /\\\ \\
/ /\\\ /\\\ \\\\\/\
\/\\\\\/\\\\\/\\\\\\
d.p.s
Richard - if you're reading this - what kind of insights did you get
from the twisted developers at pycon regarding possible integration
with pyglet?
Bruce is awesome, by the way. I really enjoyed the presentation at
pycon. A buddy of mine also did a presentation with it a while ago
when it was based on pygame - the audience was well swooned even then.
It's great to know you've rewritten it for pyglet.
Oh ... I reread your code example in your original post. My
suggestion is to *not* have multiple event loops interacting with each
other - or handing off control to other event loops. But that being
said, I'm actually confused around your `Game' loop. How does this
take care of redrawing the screen, etc? It might simplify matters to
make the Game an actor, like:
class Game:
def update(self, dt):
self.player ...
And then schedule updates on the Game for each frame via clock.
I don't think this is a very good idea, inverting control of the event
loop. It sounds messy, difficult to maintain and likely to cause
subtle, difficult-to-trace bugs.
But if you really want to give it a go, set EventLoop.has_exit = True
in the event handler when you want the loop to quit. Then pick it up
again with pyglet.app.run() when you need to. No need for coroutines.
Alex.