Status: New
Owner: ----
New issue 187 by
frik...@gmail.com: Help with interacting w/ cocos from
external thread
http://code.google.com/p/los-cocos/issues/detail?id=187
What steps will reproduce the problem?
OK. This is a tough one.
I have a director-based presentation in one file. In another I have a
simple event dispatcher that dispatches a custom event. I have successfully
imported the dispatcher and dispatched the event through a keypress. So I
know it works!
BUT here's where it gets tricky. In the second file I also have the ability
to start, in a second thread, a Flask web application that services /next/
on the localhost. I want to be able to use this to talk with my
presentation.
I have confirmed that if I import the module (which instantiates the event
dispatcher local to that file), add my presentation as an event listener to
the event dispatcher, and then start the thread, I can actually dispatch
events from that second thread which are serviced by my director-based
presentation! so far so good, kind of.
The main problem is that while I get confirmation that my event was
successfully received, cocos2d doesn't seem to handle it right. I do indeed
call "next_slide()" but the display doesn't update. When I manually do a
next_slide keypress, I can confirm that the third slide shows up,
indicating that somewhere in the background it did register the next slide
but the frame buffer didn't update.
Sometimes, only on the first time it appears, I get this error as well (but
not always):
Traceback (most recent call last):
File "/data/sv_env/local/lib/python2.7/site-packages/cocos/framegrabber.py",
line 67, in TextureGrabber
return _best_grabber()
File "/data/sv_env/local/lib/python2.7/site-packages/cocos/framegrabber.py",
line 165, in __init__
self.fbuf.check_status()
File "/data/sv_env/local/lib/python2.7/site-packages/cocos/gl_framebuffer_object.py",
line 77, in check_status
raise Exception ("Frambuffer not complete: %d" % status)
What is the expected output? What do you see instead?
Ideally, cocos2d would update the framebuffer appropriately and I'd see the
new slide and transition.
What version of the product are you using? On what operating system?
linux 3.3.7
cocos 0.5.5
pyglet 1.2alpha1
python 2.7.3
Please provide any additional information below.
Any ideas?
Either on how to get cocos/pyglet to update, or on how I can communicate to
cocos in the correct way. Queue should work but considering I'm able to
make the correct call to the correct instance object, it appears I'm doing
something right with inter-process communication, the main problem is
getting cocos to play fair.
import WebAPI
... inside my TransitionControl(cocos.layer.Layer) / __init__():
# Register Event Handler
WebAPI.eventdispatcher.push_handlers(self)
print "starting web api..."
WebAPI.runWebController() # Starts the thread
print "... success!"
method in TransitionControl:
def on_next_slide(self):
print "calling next_scene()" # I see this output, good!
self.next_scene()
WebAPI.py:
from flask import Flask, request
import pyglet.event
import threading
import time
app = Flask(__name__)
DEFAULT_PORT = 9000
running = False
class ServerQuitting(Exception): pass
class WebEventDispatcher(pyglet.event.EventDispatcher):
def next_slide(self):
self.dispatch_event('on_next_slide')
def on_next_slide(self):
print "Default Next Slide Handler"
WebEventDispatcher.register_event_type("on_next_slide")
eventdispatcher = WebEventDispatcher()
########################### url routing ###########################
@app.route("/")
def index():
return 'Hello World'
@app.route("/next/")
def next():
# Do the next thing
eventdispatcher.next_slide()
return "Sent Next Slide Event"
###################################################################
def appRunner(port):
app.run(host='0.0.0.0', port=port)
def runWebController(port=DEFAULT_PORT, debug=False):
thread = threading.Thread(target=appRunner, args=(port,))
thread.setDaemon(True)
thread.start()
return True
if __name__ == "__main__":
runWebController()
Summary:
Seems to work, but framebuffer doesn't update even though state of
application appears to update.