wait until change

Tim Hochberg tim.hochberg at ieee.org
Fri Oct 17 15:14:45 EDT 2003


Tom wrote:
> Peter Hansen wrote:
> 
>> Use time.sleep().
>>
>> -Peter
>>
> If I use sleep, I have to know how long the process should sleep, but 
> sometimes it should sleep for seconds and sometimes for minutes. So it 
> would be good if I can put it to sleep and somehow wake it up if the 
> file changed.

If you use something like::

    ctime = os.stat(file).st_ctime
    while True:
        if os.stat(file).st_ctime > ctime:
            break
        time.sleep(1.0)
    # Do stuff...

The load on your CPU will be negligible and your program will wake up at 
most 1 second after the modification

-tim





More information about the Python-list mailing list