I have a while loop which contains a process which takes roughly 1 second each time but occasionally takes over 5 minutes. I want the loop to terminate after 2 seconds in case this 5 minute scenario occurs. I have already tried
import time
t1 = time.time() + 2
while time.time() < t1:
PerformProcess
but this approach is limited by the fact that PerformProcess is automatically terminated each time time.time() is calculated.
The other solution i have commonly seen posted is
import time
t1 = time.time()
t2 = time.time()
while t2 - t1 < 2:
PerformProcess
t2 = time.time()
but this approach does not work as the refresh for t2 will not be reached if it does the 5 minute process.
I essentially just need a way to terminate the while loop after 2 seconds such that there is no time calculation done inside the while loop and/or such that the time calculation does not terminate the process within the while loop.
In case it's helpful, the PerformProcess is essentially opening a web page using urllib and extracting data from it. The simplified version of what it does is essentially
import urllib
f = urllib.urlopen(str(Hyperlink))
s = f.read()
print s
f.close()
where f is what usually takes 1 second but sometimes, for some reason, takes far longer.