0

I am fairly new to programming with Python, so forgive me if this is trivial.

I know that when programming microcontrollers it is possible to interrupt the main program (e.g. on button press or due to a timer). The interrupt leads to a code outside of the main program that is then executed. Afterwards, the main program is continued to be executed. Hereby, the interrupt handler remembers where it interrupted the main program and returns to that exact point within the code. Is it possible to implement that on Python as well?

I looked into the "threading"-library but it doesn't seem fit, since I don't want several tasks running parallel. There it seems like I have to check for an event on every second line of my main code to ensure that it really interrupts the program immediately.

If you need some context: I am implementing a program using the "PsychoPy Coder" (PsychoPy v2021.2.3) on Windows 10. I expect the program (when finished) to run for at least an hour, depending on the user. I want this program to be interrupted every 60 to 90 seconds for a "baseline task" the user has to solve. This baseline task will last for about 6 to 9 seconds and the actual program should continue afterwards. Also, I want the user to be able to abort the program with a specific button at anytime.

I would be very thankful for any hint on an elegant way of programming this :) Have a nice day!

Tobias
  • 1
  • See if `KeyboardInterrupt` is what you want. – Alex Petrosyan Sep 17 '21 at 11:33
  • In other words you would like to be able to pause the main thread, switch to another thread for a job, and then resume the main thread? That would mean you need to ensure that the 'main' thread is "interruptable" and resumable. Have a look at this: https://stackoverflow.com/questions/3262346/pausing-a-thread-using-threading-class – Jason Chia Sep 17 '21 at 11:49
  • @JasonChia Yes, this is pretty much what I want. Thanks for helping me :) – Tobias Sep 21 '21 at 09:36

0 Answers0