Creating an event loop

F

Fabian Steiner

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
 
P

Peter Hansen

Fabian said:
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
 
L

Larry Bates

Fabian said:
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/win32_how_do_i/watch_directory_for_changes.html


If it is Linux:

http://pyinotify.sourceforge.net/

-Larry Bates
 
M

Michele Simionato

Fabian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top