[Twisted-Python] Looking for a simple file event driven loop using Twisted

Jp Calderone exarkun at divmod.com
Fri Mar 25 21:28:58 MST 2005


On Fri, 25 Mar 2005 18:39:53 -0600, Ken Kinder <ken at kenkinder.com> wrote:
>It seems to me you probably will either check periodically in the 
> reactor, like this:
> 
>     def checkFile():
>         x = os.stat(file)
>         ...
>         reactor.callLater(.5, checkFile)
> 
>     reactor.callWhenRunning(checkFile)
> 
> Or just start another thread like this:
> 
>     def wakeUp():
>         (action to take when file changes)
> 
>     def checker():
>         while 1:
>             (code that waits for file to change)
>             reactor.callFromThread(wakeUp)
> 
>     reactor.callInThread(checker)

  The threaded version ties up an entire work thread from the pool for the lifetime of the application.  Unless you only want to watch one or two files, this is probably not ideal.  There's no real reason to use a thread for this at all.  Polling from the main loop is a perfectly reasonable solution (and Twisted can deal with polling at much greater frequency than twice a second, if necessary).  

  Additionally, there are actual event notification systems for this: dnotify and inotify on Linux, for example.  Twisted doesn't support these directly (although it might support inotify soon), but integrating them and other things like them is not extremely difficult.

  Jp




More information about the Twisted-Python mailing list