4

I currently have a program that runs at regular intervals. Right now the program runs continuously, checking for new files every 30 minutes:

def filechecker():
    #check for new files in a directory and do stuff

while True:
    filechecker()
    print '{} : Sleeping...press Ctrl+C to stop.'.format(time.ctime())
    time.sleep(1800)

However, I'd also like the user to be able to come to the terminal and enter a keystroke to manually call filechecker() instead of either waiting for the program from waking from sleep or having to relaunch the program. Is this possible to do? I tried to look at using threading, but I couldn't really figure out how to wake the computer from sleep (don't have much experience with threading).

I know I could just as easily do :

while True:
    filechecker()
    raw_input('Press any key to continue')

for full manual control, but I want my to have my cake and eat it too.

1
  • This... this.. is community wiki gold... this could go viral... Commented Apr 24, 2015 at 22:28

3 Answers 3

4

You can use a try/except block with KeyboardInterrupt (which is what you get with Ctrl-C while in the time.sleep(). Then in the except, ask the user if they want to quit or run filechecker immediately. Like:

while True:
    filechecker()
    try:
        print '{0} : Sleeping...press Ctrl+C to stop.'.format(time.ctime())
        time.sleep(10)
    except KeyboardInterrupt:
        input = raw_input('Got keyboard interrupt, to exit press Q, to run, press anything else\n')
        if input.lower() == 'q':
            break
4
  • I would add that you should have a control boolean on the while loop, which if input.lower() == 'q': control = False. Same result, but a bit better structured :)
    – Matthew
    Commented Apr 24, 2015 at 22:10
  • That's a clever way to handle it. Wouldn't have thought of using an exception. Commented Apr 24, 2015 at 22:11
  • Isn't this pretty much what I did?
    – Ace
    Commented Apr 25, 2015 at 19:36
  • @Ace yes. You posted your answer between when I read the question and posted my answer. If I'd seen your answer before I posted I would have just commented on yours. But at this point I think I like nickie's answer best, as the most complete. Commented Apr 26, 2015 at 21:14
3

The solution provided by clindseysmith introduces one more key press than what the original question was asking for (at least, my interpretation thereof). If you really want to combine the effect of the two snippets of code in the question, i.e., you don't want to have to press Ctrl+C for calling the file checker immediately, here's what you can do:

import time, threading

def filechecker():
    #check for new files in a directory and do stuff
    print "{} : called!".format(time.ctime())

INTERVAL = 5 or 1800
t = None

def schedule():
    filechecker()
    global t
    t = threading.Timer(INTERVAL, schedule)
    t.start()

try:
    while True:
        schedule()
        print '{} : Sleeping... Press Ctrl+C or Enter!'.format(time.ctime())
        i = raw_input()
        t.cancel()
except KeyboardInterrupt:
    print '{} : Stopped.'.format(time.ctime())
    if t: t.cancel()

The variable t holds the id of the thread that is scheduled next to call the file checker. Pressing Enter cancels t and reschedules. Pressing Ctrl-C cancels t and stops.

1
  • This does exactly what I had in mind. I really like what @clindseysmith did as well, but you win the checkmark. I also learned a little bit more about how threading works. Thanks!
    – qRTPCR
    Commented Apr 24, 2015 at 23:45
0

You could make it so that by pressing ctrl+c filechecker() would run by using this:

def filechecker():
    #check for new files in a directory and do stuff

filechecker()
while True:
    print '{} : Sleeping...press Ctrl+C to run manually.'.format(time.ctime())
    try:
        time.sleep(1800)
    except KeyboardInterrupt:
        filechecker()
   filechecker()
2
  • That will run filechecker(), then immediately run it again. Not the end of the world, I guess. But, pass in the except clause will also work. Commented Apr 24, 2015 at 22:02
  • I fixed it so that it won't immediately run it again, but will have the same result.
    – Ace
    Commented Apr 25, 2015 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.