Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Python script for a user-specified amount of time

Tags:

python

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
like image 625
Rob Avatar asked May 14 '10 04:05

Rob


People also ask

How do I run a program for a specific time in Python?

Method 1: Using Time Module We can create a Python script that will be executed at every particular time. We will pass the given interval in the time. sleep() function and make while loop is true. The function will sleep for the given time interval.

How do you check Python running time?

You can use the Python profiler cProfile to measure CPU time and additionally how much time is spent inside each function and how many times each function is called. This is very useful if you want to improve performance of your script without knowing where to start.

How do I run a Python program multiple times?

Use the oml. index_apply function to run a Python function multiple times in Python engines spawned by the database environment. The times argument is an int that specifies the number of times to run the func function. The func argument is the function to run.


3 Answers

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.

like image 81
Eli Courtwright Avatar answered Oct 19 '22 15:10

Eli Courtwright


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.

like image 20
Etaoin Avatar answered Oct 19 '22 15:10

Etaoin


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'
like image 45
johntellsall Avatar answered Oct 19 '22 14:10

johntellsall