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.
Asked
Active
Viewed 1,044 times
0
-
You may want to consider adding a timeout to subprocess: http://stackoverflow.com/questions/1191374/subprocess-with-timeout – rubik Sep 07 '12 at 14:50
-
its not clear what your asking... are you launching with subroccess ? you can do `if time.time()-starttime > somevalue:return` in side of a loop in a function... – Joran Beasley Sep 07 '12 at 14:50
-
related: [Stop reading process output in Python without hang?](http://stackoverflow.com/a/4418891/4279) – jfs Sep 07 '12 at 14:54
1 Answers
1
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)

jfs
- 399,953
- 195
- 994
- 1,670