Hi, I’m an embedded developer and trying to write some python software for a personal project (A bot for an idle game).
One concept I’m struggling with is asynchronous behavior and interrupts on desktop systems. I’m not really finding any good resources. I’m hoping one of you guys can explain this in a way that I get it or provide me some good resources to read.
What I want to do is pretty simple. I want to have a super loop around my software which runs until a condition is met (A specific key is pressed). I’d rather not use polling, requesting an input will block the software and require user input each loop. I’ve tried reading the keyboard state directly but the packages I used either didn’t find my keyboard or required root access.
My preferred attempt would have been to register something like an interrupt handler which is called when a keyboard event is detected. The general suggestion on the internet for interrupts in python is the signal package. This however seems to only be for dealing with exceptions, not general interrupts.
Are interrupts for general events like I/O even a thing on desktops? And if so, how would I go about interacting with them from my code?
This is OS-level functionality, and usually is OS-specific in my experience.
On Linux, the entire “signal” API handles this. IIRC, Python has access to the signal API.
Low level Win32 has a poll-loop with an assumed getMessage() call, so a WaitForSingleObject kind of call in Win32 often does the trick.
Higher level C# have async features that can replicate the feature far more easily, but I’m ignorant about the C# world.
Because of differences between Linux and Windows, Python signals don’t have as much platform independence as you might like.