[Twisted-Python] Looking for a simple file event driven loop using Twisted
Hello, I'm looking for some example code I can use to build an app from. I need to 'wait' or sleep for new data in a file. Then wake and process the data. The file is a regular file, not a pipe. It is the output of a existing program (i.e., not mine to modify). Needless to say, the API for hugh. I'm need some help finding the right tools. Thanks, Steven Howe
steven howe wrote:
Hello, I'm looking for some example code I can use to build an app from. I need to 'wait' or sleep for new data in a file. Then wake and process the data.
The file is a regular file, not a pipe. It is the output of a existing
As far as I remember off-hand, the "tail -f" trick is this: every once in a while (use LoopingCall or reactor.callLater), read until eof, and do f.seek(0, 1) to reset the EOF.
program (i.e., not mine to modify). Needless to say, the API for hugh.
Huh?
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) -Ken steven howe wrote:
Hello, I'm looking for some example code I can use to build an app from. I need to 'wait' or sleep for new data in a file. Then wake and process the data.
The file is a regular file, not a pipe. It is the output of a existing program (i.e., not mine to modify). Needless to say, the API for hugh. I'm need some help finding the right tools.
Thanks,
Steven Howe
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Fri, 25 Mar 2005 18:39:53 -0600, Ken Kinder <ken@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
Using Ken's first example, here's what I came up with: import os import stat from twisted.internet import reactor class FileWatcher: def __init__(self, period, filename): self.period = period self.filename = filename self.callbacks = [] self.mtime = os.stat(filename)[stat.ST_MTIME] reactor.callWhenRunning(self.checkFile) def addCallback(self, callback): self.callbacks.append(callback) def removeCallback(self, callback): self.callbacks.remove(callback) def checkFile(self): mtime = os.stat(self.filename)[stat.ST_MTIME] if mtime > self.mtime: self.mtime = mtime for callback in self.callbacks: callback(self.filename, mtime) reactor.callLater(self.period, self.checkFile) if __name__=='__main__': import time def changed(filename, mtime): print "%s modified at %s." % (filename, time.ctime(mtime)) open('/tmp/foo', "w").close() FileWatcher(0.1, '/tmp/foo').addCallback(changed) reactor.run() Run it in one terminal window, and in the other run touch /tmp/foo to see what happens.
Looks like a winner. Thank you very much. Steven On Fri, 25 Mar 2005 23:57:07 -0800, Dave Cook <daverz@gmail.com> wrote:
Using Ken's first example, here's what I came up with:
import os import stat from twisted.internet import reactor
class FileWatcher:
def __init__(self, period, filename): self.period = period self.filename = filename self.callbacks = [] self.mtime = os.stat(filename)[stat.ST_MTIME] reactor.callWhenRunning(self.checkFile)
def addCallback(self, callback): self.callbacks.append(callback)
def removeCallback(self, callback): self.callbacks.remove(callback)
def checkFile(self): mtime = os.stat(self.filename)[stat.ST_MTIME] if mtime > self.mtime: self.mtime = mtime for callback in self.callbacks: callback(self.filename, mtime) reactor.callLater(self.period, self.checkFile)
if __name__=='__main__':
import time
def changed(filename, mtime): print "%s modified at %s." % (filename, time.ctime(mtime))
open('/tmp/foo', "w").close() FileWatcher(0.1, '/tmp/foo').addCallback(changed) reactor.run()
Run it in one terminal window, and in the other run
touch /tmp/foo
to see what happens.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
This version allows you to stop the event loop, to register a callback, and to specify which stat items you want to watch. Dave Cook
participants (5)
-
Dave Cook
-
Jp Calderone
-
Ken Kinder
-
steven howe
-
Tommi Virtanen