scheduler or infinite loop

Frank Millman frank at
Wed Sep 29 15:04:23 CEST 2010

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)
>    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('')
>        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):
        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

    def stop(self):

In the startup of the main program, insert the following -

    data_getter = DataGetter()

At the end of the program, terminate the loop like this -


Frank Millman

More information about the Python-list mailing list