Graceful exiting of a program in Python?

python

Solution

The best way is to rewrite the script so it doesn't use `while True:`.

Sadly, it's impossible to conjecture a good way to terminate this.

You could use the Linux signals.

You could use a timer and stop after a while.

You could have `dostuff` return a value and stop if the value is `False`.

You could check for a local file and stop if the file exists.

You could check an FTP site for a remote file and stop of the file exists.

You could check an HTTP web page for information that indicates if your loop should stop or not stop.

You could use OS-specific things like semaphores or shared memory.

Problem

I have a script that runs as a ``` while True: doStuff() ``` What is the best way to communicate with this script if I need to stop it but I don't want to kill it if it is in the middle of an operation?

Original source