scheduler or infinite loop
John Nagle
nagle at animats.com
Thu Sep 30 01:24:53 EDT 2010
On 9/29/2010 4:59 AM, harryos wrote:
> hi
> I am trying to write a program to read data from a site url.
> The program must read the data from site every 5 minutes.
>
> def get_data_from_site(pageurlstr):
> h=urllib.urlopen(pageurlstr)
> data=h.read()
> process_data(data)
A key point here is that you're not handling network
failures. The first time you have a brief network
outage, your program will fail.
Consider something like this:
def get_data_from_site(pageurlstr):
try :
h=urllib.urlopen(pageurlstr)
data=h.read()
except EnvironmentError as message :
print("Error reading %s from network at %s: %s" %
(pageurlstr, time.asctime(), str(message))
return(False)
process_data(data)
return(True)
lastpoll = 0 # time of last successful read
POLLINTERVAL = 300.0
RETRYINTERVAL = 30.0
while True:
status = get_data_from_site('http://somesite.com/')
if not status : # if fail
time.sleep(RETRYINTERVAL) # try again soon
print("Retrying...")
continue
now = time.time() # success
# Wait for the next poll period. Compensate for how
# long the read took.
waittime = max(POLLINTERVAL - (now - lastpoll), 1.0)
lastpoll = now
time.sleep(waittime)
More information about the Python-list
mailing list