tags:

views:

135

answers:

3

Sorry, this is probably a terrible question. I've JUST started learning python today. I've been reading a Byte of Python. Right now I have a project for Python that involves time. I can't find anything relating to time in Byte of Python, so I'll ask you:

How can I run a block for a user specified amount of time and then break?

For example (in some pseudo-code):

time = int(raw_input('Enter the amount of seconds you want to run this: '))
while there is still time left:
    #run this block

or even better:

import sys
time = sys.argv[1]
while there is still time left:
    #run this block

Thanks for any help. Also, additional online guides and tutorials would be much appreciated. I really like Byte of Python. Dive into Python can't quite hold my attention, though. I suppose I should suck it up and try harder to read that one.

+4  A: 

Try time.time(), which returns the current time as the number of seconds since a set time called the epoch (midnight on Jan. 1, 1970 for many computers). Here's one way to use it:

import time

max_time = int(raw_input('Enter the amount of seconds you want to run this: '))
start_time = time.time()  # remember when we started
while (time.time() - start_time) < max_time:
    do_stuff()

So we'll loop as long as the time since we started is less than the user-specified maximum. This isn't perfect: most notably, if do_stuff() takes a long time, we won't stop until it finishes and we discover that we're past our deadline. If you need to be able to interrupt a task in progress as soon as the time elapses, the problem gets more complicated.

Etaoin
I do need to be able to interrupt the task in progress, actually. But wouldn't that be a simple modification such as `if (time.time() - start_time) > max_time: break`? Please correct me if I'm wrong
Rob
Sure! That'll work fine -- you just need to make sure you run that test in between small chunks of execution.
Etaoin
Thanks a lot, Etaoin!
Rob
+3  A: 

I recommend spawning another thread, making it a daemon thread, then sleeping until you want the task to die. For example:

from time import sleep
from threading import Thread

def some_task():
    while True:
        pass

t = Thread(target=some_task)  # run the some_task function in another
                              # thread
t.daemon = True               # Python will exit when the main thread
                              # exits, even if this thread is still
                              # running
t.start()

snooziness = int(raw_input('Enter the amount of seconds you want to run this: '))
sleep(snooziness)

# Since this is the end of the script, Python will now exit.  If we
# still had any other non-daemon threads running, we wouldn't exit.
# However, since our task is a daemon thread, Python will exit even if
# it's still going.

The Python interpreter will shut down when all non-daemon threads have exited. So when your main thread exits, if the only other thread running is your task that you are running in a separate daemon thread, then Python will just exit. This is a convenient way of running something in the background if you want to be able to just quit without worrying about manually causing it to quit and waiting for it to stop.

So in other words, the advantage which this approach has over using sleep in a for loop is in that case you have to code your task in such a way that it's broken up into discrete chunks and then check every so often whether your time is up. Which might be fine for your purposes, but it can have problems, such as if each chunk takes a significant amount of time, thus causing your program to run for significantly longer than what the user entered, etc. Whether this is a problem for you depends on the task you are writing, but I figured I would mention this approach in case it would be better for you.

Eli Courtwright
I'm sorry, can you comment some of that? I haven't quite ventured into threading and sleeping. From what I understand by your answer: waking from sleep breaks the thread?
Rob
@Rob: I've added more explanation and several comments to the code. Let me know if anything in particular is unclear.
Eli Courtwright
Actually you've made it all very clear. This seems a lot more efficient than checking the time repeatedly. Thanks a lot
Rob
A: 

If you're on Linux, and you want to interrupt a long-running process, use signal:

import signal, time

def got_alarm(signum, frame):
    print 'Alarm!'

# call 'got_alarm' in two seconds:
signal.signal(signal.SIGALRM, got_alarm)
signal.alarm(2)

print 'sleeping...'
time.sleep(4)

print 'done'
shavenwarthog