I have a script that controls an external machine with rather slow i/o operations. I'd like to be able to suspend and resume execution by pressing a key at any time, in such a way that execution of the current iteration in the loop is first completed, and the loop pauses before going on to the next iteration.
for i in iterable:
foo(i)
## Pause if p is pressed
## resume if p is pressed again
Specifically, as the foo() subroutine takes several seconds, I would like the programme to listen for the key press even while it is executed. Is there a simple way to achieve this?
suspend python process by pressing hotkey F8:
Source: https://www.reddit.com/r/learnpython/comments/onc1jr/how_do_you_pause_and_continue_script_on_keypress/