简体   繁体   中英

How do you interrupt execution of a function in a child process, to play and pause audio with Pyglet?

I'm writing an application in Python (definitely not suited to the task, but I'm doing it anyway). Part of the application requires an audio player, for which I'm using Pyglet as it seems to be the easiest to use and does what I need it to be able to do (load OGGs, MP3s, and WAVs; play audio; pause audio; get the current timestamp).

I understand that I will need to run the audio stuff in a separate child process (or however multiprocessing handles this, a subprocess I suppose). However, running pyglet.app.run() essentially locks the process until the audio has finished playing. I cannot see a way to get the process to pause, as you can't access the process' address space from outside the process (so I can't run the process and then run player.pause()).

I'm just a bit stumped in general and would appreciate a rundown of how to get multiprocessing working with Pyglet so I can play and pause audio at will (this is all being implemented into a GUI, which handles a whole bunch of other things as well).

Like most GUI frameworks, pyglet uses a so-called "event loop". This is the thing that's started with pyglet.app.run() . It indeed blocks the current thread (not process) and schedules all the events that happen within the GUI framework. If you want to run your own stuff in between, you have to do it via the event loop, see https://pyglet.readthedocs.io/en/latest/programming_guide/time.html#guide-calling-functions-periodically .

Alternatively, you can try to create a new thread (not process), but then you have to take care when accessing things that are potentially also accessed from the pyglet event loop.

GUI frameworks are typically single-threaded and are often not thread-safe (I don't know details about pyglet). If you create an additional thread, you have to be very careful when interacting with the event loop from the other thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM