Florin Dita
Florin Dita

Reputation: 109

How to execute a code for a given time period

I want to know if there is a way in python 2.7 to run code for just for a given time, like 3600 seconds. The time can variate from one execution to an other. Any ideas are welcome since I am stuck with this issue.

Upvotes: 0

Views: 1146

Answers (1)

jfs
jfs

Reputation: 414089

To allow to interrupt the computations for any reason:

def compute_something(stopped):
    while not stopped:
        # continue computations

stopped = []
threading.Timer(3600, stopped.append, args=[True]).start()
compute_something(stopped)

Upvotes: 1

Related Questions