Created
April 3, 2014 03:05
-
-
Save meejah/9947596 to your computer and use it in GitHub Desktop.
"tail -f" style file-reading in Twisted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
from twisted.internet.interfaces import IReadDescriptor | |
from twisted.internet.fdesc import setNonBlocking | |
from twisted.internet import reactor | |
from zope.interface import implementer | |
@implementer(IReadDescriptor) | |
class Blam(object): | |
def __init__(self, f): | |
self.file = f | |
setNonBlocking(self.file) | |
def logPrefix(self): | |
return 'blam' | |
def doRead(self): | |
data = self.file.read() | |
if data: | |
print "it's dataz", data | |
def fileno(self): | |
return self.file.fileno() | |
def connectionLost(self, why): | |
print "closed", why | |
f = Blam(open(sys.argv[1], 'r')) | |
reactor.addReader(f) | |
print "go!", sys.argv[1] | |
reactor.run() |
Hmm, it seems to be using 100% CPU on Debian, also -- clearly something is missing...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
does not notice new lines on OSX
uses 100% cpu