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)
At first, I thought of using the sched module ,but then it doesn't
look right adding so many scheduler.enter() statements.The program is
supposed to execute the above function every
5 minutes until the application is shut down by the user.
I created an infinite loop
while True:
print time.asctime()
get_data_from_site('http://somesite.com/')
time.sleep(300)
Is there a better way to do this?Any suggestions ,pointers most
welcome
thanks
harry
Here is a technique that allows the loop to run in the background, in its
own thread, leaving the main program to do other processing -
import threading
class DataGetter(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()
def run(self):
event = self.event # make local
while not event.is_set():
print time.asctime()
# either call the function from here,
# or put the body of the function here
get_data_from_site('http://somesite.com/')
event.wait(300)
def stop(self):
self.event.set()
In the startup of the main program, insert the following -
data_getter = DataGetter()
data_getter.start()
At the end of the program, terminate the loop like this -
data_getter.stop()
HTH
Frank Millman
--
http://mail.python.org/mailman/listinfo/python-list