Interrupting a continuous script

Kragen Sitaker kragen at pobox.com
Mon Jun 10 17:49:21 EDT 2002


"Julian Gollop" <julian.gollop at ntlworld.com> writes:
> I have a few programs continually processing text files on a Linux machine.
> Occasionally I need to stop them, but I don't want to use a keyboard
> interrupt because it may cause the script to stop in the middle of doing
> something.
> Does anyone know a simple way to allow me to stop the process by pressing a
> key on the keyboard - without stopping inside something crucial?

You could use select() to see if stdin is readable.

My suggestion, though, would be to try to design the program so that
it can be stopped at any point without anything bad happening.  If the
machine gets unplugged, after all...

There are two general strategies for this: restartability, or
idempotence, and atomicity.

A restartable operation is one that, if interrupted in the middle and
restarted from the beginning, will produce the same results as if it
had been done once straight through.

An atomic operation is one that is either completed or not and cannot
be interrupted in the middle.  On Linux, renaming a file and creating
a directory are atomic operations.




More information about the Python-list mailing list