Creating an event loop
Michele Simionato
michele.simionato at gmail.com
Tue Apr 11 10:38:55 EDT 2006
Fabian Steiner wrote:
> Hello!
>
> I am currently wondering how to write something like an "event loop".
> For example, if I want to write a function that checks whether a file
> was added or removed in a directory I would think of a "while 1: ..."
> construct that checks the mtime of the directory. Is this the right way
> to achieve the exepected result or are there any better ways?
Well, if you feel like cheating, you could use the Tkinter mainloop:
import os
from Tkinter import Tk
root = None # invisible tk window
DELTA_T = 10000 # 10 seconds
def checkdir(path='.'):
print os.listdir(path) # do whatever check you wish
root.after(DELTA_T, checkdir)
if __name__ == '__main__':
root = Tk()
root.withdraw()
checkdir()
try:
root.mainloop()
except KeyboardInterrupt:
pass
The advantage is that you can every easily schedule recurring and
non-recurring events.
Michele Simionato
More information about the Python-list
mailing list