Why can't pyglet get the events from another thread?

92 views
Skip to first unread message

杨金骉

unread,
Jul 2, 2017, 10:29:12 AM7/2/17
to pyglet-users
I'm building a real-time sound controller.
I tried to put the sound player and detector of keypress in different threads, and used a variable as the messenger. Below is the demo code:

## detector
def
wait():
   
while True:
       
win.dispatch_events()

                     shared
.pressing = e['key'] # the got key

       
if shared.figure_released:
            shared
.pressing = None

       
time.sleep(0.01)


td
= threading.Thread(target=wait)
td
.start()

## player
while 1:
   
if shared.pressing=='k':
       
# pitch up
   
elif shared.pressing=='j':
       
# pitch down

And then I got the error:
RuntimeError: EventLoop.run() must be called from the same thread that imports pyglet.app


Why can't pyglet get the events from another thread?

Benjamin Moran

unread,
Jul 3, 2017, 1:00:35 AM7/3/17
to pyglet-users
Are you using pyglet.app.run() in your code?

杨金骉

unread,
Jul 3, 2017, 1:04:04 AM7/3/17
to pyglet...@googlegroups.com
No. I just used loop + dispatch_events() manually.


--
You received this message because you are subscribed to a topic in the Google Groups "pyglet-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyglet-users/PrD2Z8O1qnI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyglet-users+unsubscribe@googlegroups.com.
To post to this group, send email to pyglet...@googlegroups.com.
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.

Benjamin Moran

unread,
Jul 4, 2017, 12:15:49 AM7/4/17
to pyglet-users
I don't think win.dispatch_events() will work unless it's on the main thread, but I haven't look at this code recently.
Is it necessary to create your own event loop? The Player class already uses threads internally for playback.

There is an example in the pyglet repositories that might be useful:
https://bitbucket.org/pyglet/pyglet/src/aca0272d8f04e75f345272f8e1b5027ec8091586/examples/synthesizer.py?at=default&fileviewer=file-view-default

If that doesn't help, could you descrbe your project more?
To unsubscribe from this group and all its topics, send an email to pyglet-users...@googlegroups.com.

杨金骉

unread,
Jul 4, 2017, 1:27:15 AM7/4/17
to pyglet...@googlegroups.com
Thanks for your reply!

Is it necessary to create your own event loop? 
I built a toolbox for the psychological/neuroscience experiments. The pyglet.app.run() might confuse the user so I sidestepped it and made a flat way (e.g. http://expy.readthedocs.io/en/latest/quickstart/) for the coders of experiments.

- My  project
I'm writing a function that enables the experiment participants to adjust the volume or pitch of the sound output by pressing keyboard continuously. 
In that way, I need the sound playing track and the keyboard sensor working in different threads.
I'll be appreciated if you have the better solution!


Best regards,
Jinbiao Yang(杨金骉)

-----------------------------------------------------------------------------
Cell Phone: +86 13162513165

Research Associate,
NYU-ECNU Institute of Brain and Cognitive Science,
NYU Shanghai,
3663 North Zhongshan Road, Shanghai, China 200062

To unsubscribe from this group and all its topics, send an email to pyglet-users+unsubscribe@googlegroups.com.

Benjamin Moran

unread,
Jul 4, 2017, 3:06:31 AM7/4/17
to pyglet-users
For instant key press feedback, the window.on_key_press event is the easiest method.
If you want to check for held-in keys, maybe a KeyStateHandler would work.
http://pyglet.readthedocs.io/en/latest/programming_guide/keyboard.html#remembering-key-state

You could then make a function to query the key state:

from pyglet.window import key

window = pyglet.window.Window()
keys = key.KeyStateHandler()
window.push_handlers(keys)

def check_key_status(dt):
   
global keys
   
if keys[key.J]:
        do_something
()

pyglet
.clock.schedule_interval(check_key_status, 1/60.0)



Would that work?

Rob van der Most

unread,
Jul 4, 2017, 5:48:01 AM7/4/17
to pyglet...@googlegroups.com
I believe the application event loop and the current way of processing the events was introduced somewhere in version 1.1.x. The legacy way of processing events, where you need to roll your own event loop, is still present. Unfortunately there are some inconsistencies between both and this sometimes bites.

For the next release I think we should clean this up and make sure there is only 1 implementation for handling the events that is suitable for both the builtin event loop and custom event loops.

Rob

You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyglet-users+unsubscribe@googlegroups.com.

杨金骉

unread,
Jul 4, 2017, 6:21:57 AM7/4/17
to pyglet...@googlegroups.com
I'm afriad not. 
I want the sound keep playing in the period no matter I press, hold, or release the key. But your solution will stop the sound when the event handler (check_key_status) working.


Best regards,
Jinbiao Yang(杨金骉)

-----------------------------------------------------------------------------
Cell Phone: +86 13162513165

Research Associate,
NYU-ECNU Institute of Brain and Cognitive Science,
NYU Shanghai,
3663 North Zhongshan Road, Shanghai, China 200062

To unsubscribe from this group and all its topics, send an email to pyglet-users+unsubscribe@googlegroups.com.

杨金骉

unread,
Jul 4, 2017, 6:24:28 AM7/4/17
to pyglet...@googlegroups.com
@Rob, would the new implementation allow me to accomplish my need?


Best regards,
Jinbiao Yang(杨金骉)

-----------------------------------------------------------------------------
Cell Phone: +86 13162513165

Research Associate,
NYU-ECNU Institute of Brain and Cognitive Science,
NYU Shanghai,
3663 North Zhongshan Road, Shanghai, China 200062

Rob van der Most

unread,
Jul 4, 2017, 12:34:56 PM7/4/17
to pyglet...@googlegroups.com
It should in the end. But unfortunately I think that is currently not the case yet.

Rob

--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyglet-users+unsubscribe@googlegroups.com.

杨金骉

unread,
Jul 4, 2017, 12:46:19 PM7/4/17
to pyglet...@googlegroups.com
Okay. I'll put the event handler into the main thread and put the sound player into another thread. It would look much more ugly but might work.
Thanks for your reply!


Best regards,
Jinbiao Yang(杨金骉)

-----------------------------------------------------------------------------
Cell Phone: +86 13162513165

Research Associate,
NYU-ECNU Institute of Brain and Cognitive Science,
NYU Shanghai,
3663 North Zhongshan Road, Shanghai, China 200062

Reply all
Reply to author
Forward
0 new messages