this post was submitted on 20 Jun 2023
2 points (100.0% liked)

Programming

18018 readers
93 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
 

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?

top 1 comments
sorted by: hot top controversial new old
[–] dragontamer 1 points 2 years ago* (last edited 2 years ago)

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.