[Tutor] "Ctrl-C (unix)" in python

John Fouhy john at fouhy.net
Thu Feb 19 00:44:39 CET 2009


2009/2/19 pa yo <payo2000 at gmail.com>:
> I am  running my Twitter>>Wiki bots in infinite loops but can't find
> an easy way to turn them off gracefully once I have started them. At
> the moment I have to go into the terminal window where they are
> running and type "Ctrl-C". (I am running Ubuntu 8.10 and python 2.5.2)
>
> I was thinking that I could get the bot script to read a text file at
> the start of the main loop and have a separate script writing the
> "exit" order to the same text file.... but before I get too involved
> with this I wanted to know if there  was an  built-in function to
> switch scripts on and off.

You could go multithreaded.  Something like this:

In your main thread, replace

    while True:

with

    while not self.done:

and then have another thread that does something like:

while True:
    response = raw_input('Type "quit" to quit:')
    if response.lower() == 'quit':
        self.done = True
        break

Or even more simply:

raw_input("Hit return to quit.")
self.done = True

It would also be easy to replace this with, say, Tkinter code to put a
"Quit" button in a window on your desktop.

-- 
John.


More information about the Tutor mailing list