Skip to content

Instantly share code, notes, and snippets.

@meejah meejah/blam.py
Created Apr 3, 2014

Embed
What would you like to do?
"tail -f" style file-reading in Twisted
#!/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()
@kneufeld

This comment has been minimized.

Copy link

kneufeld commented Apr 3, 2014

does not notice new lines on OSX
uses 100% cpu

@meejah

This comment has been minimized.

Copy link
Owner Author

meejah commented Apr 3, 2014

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
You can’t perform that action at this time.