By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,363 Members | 1,708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,363 IT Pros & Developers. It's quick & easy.

Creating an event loop

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.