469,266 Members | 2,023 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,266 developers. It's quick & easy.

Creating an event loop

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?

Cheers,
Fabian
Apr 9 '06 #1
3 4182
Fabian Steiner wrote:
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?


That's fine for a start, provided you always insert a small time.sleep()
call so that you don't use up all the CPU time wastefully checking for
something that happens very rarely. In the end, it's a tradeoff between
CPU usage and "latency", because if you use (for example) time.sleep(5),
you won't waste much time but you will also take up to 5 seconds to
notice that the directory has changed.

Usually values like 0.1 (100ms) work well for things that need a quick
response, while 2s works nicely for things that don't. Even Python is
pretty unnoticeable if it wakes up only every two seconds for a brief
bit of processing.

-Peter

Apr 9 '06 #2
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?

Cheers,
Fabian


You didn't mention what platform you were working on. Google turns up
the following items.

These links might be beneficial if it is Windows:

http://tgolden.sc.sabren.com/python/...r_changes.html
If it is Linux:

http://pyinotify.sourceforge.net/

-Larry Bates
Apr 10 '06 #3
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

Apr 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tristan Ludowyk | last post: by
21 posts views Thread by Alo Sarv | last post: by
10 posts views Thread by Dee | last post: by
4 posts views Thread by NutJob | last post: by
6 posts views Thread by Mike MacSween | last post: by
3 posts views Thread by Garth Wells | last post: by
13 posts views Thread by LordHog | last post: by
2 posts views Thread by Jay Dee | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.