r/learnpython • u/justonium • Feb 25 '15
How can I queue up custom Pyglet events while keeping input and graphics events at highest priority?
Context: I'm writing an application that takes keyboard input, and displays graphics.
Ok, so there's an effectively infinitely long list of low priority function calls which have to be made. I would like the keyboard triggered events to continue executing right on time, and I would like the display to continue updating on time, but, when neither of these two things need to be done, I would like all remaining CPU time to be used to continue to walk down the list of low priority function calls, making them one by one.
I tried to implement this behavior by recursively calling dispatch_event in the last line of the event handler that handles the low priority events. This call is recursive because the type of event being dispatched is another of the same type of low priority events. The code in the body of this event handler simply pops a function call off of the long list, and executes it.
However, this doesn't work, because it turns out that dispatch_event actually fires the event's event handler immediately, as opposed to adding it to a queue. So, the result is that execution becomes stuck in a recursive loop, resulting in a stack overflow.
1
u/justonium Apr 25 '15
I also asked on stackoverflow: link
1
u/Doormatty I get paid to write Python! Feb 25 '15
It feels like you're wanting to write your own event handlers loop.
http://pyglet.org/doc-current/programming_guide/eventloop.html
1
u/elbiot May 22 '15
What you want is something asynchronous. Python has a GIL, which means you can't be running two functions simultaneously. What you want is to run multiple python instances simultaneously: one that handles the keyboard input and GUI, and one that is doing the heavy stuff. The one that handles the GUI receives the output of the heavy stuff when it is ready and displays it.
the multiprocessing library (I hear) does this for you. In the gui (pyglet) instance, you'd set the framerate to something reasonable, and dispatch heavy stuff to another instance as needed. Every tick, if the heavy stuff has a result, it would be available. Otherwise, both keep plugging along independently.
You could schedule a check_heavy_stuff function every 300ms or so, or put it in your render function.