0

I am developing a program to control a machine. Frequently the movement needs to be stopped before it does harm. At the moment, immediate control is by switching off the power because the machine continues to respond to stored commands. This leaves a lot of loose ends. I need to send a command to stop the machine immediately, and clear the queue before it can be started up. This can happen at any time/place during the running of the program.

All the examples I have seen here appear to assume that the placing of the C and keyboardinterrupt response is predicable, e.g. How do I capture SIGINT in Python?

How do I capture C at any (unpredicted) point in the program?

This question reveals that I don't really understand how the underlying processes work.

1 Answers1

0

You could execute all of your code within a "main" function and catch it within an except.

def Main():
    # Running code...

try:
    Main()
except KeyboardInterrupt:
    # Execute actions to stop immediately.
except Exception as e:
    print("An unexpected error occurred:\n" + str(e)) 
    # Execute actions to stop immediately.
difurious
  • 1,523
  • 3
  • 19
  • 32
  • Thank you. The underlying philosophy of Python is interesting. I should have thought that a program would appoint a watchman, who would report a danger to the program. This method assumes that the watchman is primary, with the program a subordinate activity. – cvhManchester Aug 10 '20 at 13:00
  • Did it answer your question? If so don't forget to accept the answer https://stackoverflow.com/help/someone-answers :) – difurious Aug 10 '20 at 17:23