[Twisted-Python] How does the pollreactor work?
Hi, there. I have been trying to figure out how to use the twisted.internet.pollreactor so that I can use it to monitor a Linux named PIPE (i.e. a FIFO created using mkfifo()). I tried without success to Google some examples but I couldn't find anything. Now, for a simple TCP server, the setup looks something like this (i.e. like the QOTD example in the documentation): from twisted.internet.protocol import Protocol, Factory from twisted.internet import reactor class QOTD(Protocol): def connectionMade(self): self.transport.write("An apple a day keeps the doctor away\r\n") self.transport.loseConnection() # Next lines are magic: factory = Factory() factory.protocol = QOTD # 8007 is the port you want to run under. Choose something >1024 reactor.listenTCP(8007, factory) reactor.run() So, the TCP type of reactor has something that tells it to listen to port 8007 and use some form of factory or object to take care of things when a connection occurs. As far as I can guess, I would need to do some steps like this (NOTE: using mixture of real and pseudo code): from twisted.internet import pollreactor pollreactor.install() <create a FileDescriptor object using /tmp/myfifo, maybe a class?> <use the reactor.addReader() call to register the FileDescriptor object with the reactor> <call reactor.run() (no loop) or call reactor.doPoll() (in a loop, processing returned events)?> Anyways, if anyone can give me some direction on how this reactor works. Ideally, I would assume that the relationships would be somewhat like this: Protocol analogous to FileDescriptor reactor.listenTCP analogous to reactor.addReader() reactor.run() would invoke the appropriate call in my FileDescriptor-based class to handle any read events on /tmp/myfifo Thanks in advance. -Rodney
On 03:35 pm, rlott@evertz.com wrote:
I have been trying to figure out how to use the twisted.internet.pollreactor so that I can use it to monitor a Linux named PIPE (i.e. a FIFO created using mkfifo()). I tried without success to Google some examples but I couldn't find anything.
This doesn't really have anything to do with pollreactor. Any reactor that implements IReactorFDSet can monitor a pipe. It was easier for me to implement this than to explain what to do, so here's the code for monitoring a named pipe for read, given its name. I'm pretty sure it's not 100% correct, but if you want to read about handling I/O events from fifos on your platform, you are welcome to do so. import os from twisted.internet.abstract import FileDescriptor from twisted.internet.main import CONNECTION_DONE class FIFODescriptor(FileDescriptor): def __init__(self, reactor, fifoname, protocol): FileDescriptor.__init__(self, reactor) self.fifo = os.open(fifoname, os.O_NONBLOCK | os.O_RDONLY) self.protocol = protocol self.protocol.makeConnection(self) def connectionLost(self, reason): self.protocol.connectionLost(reason) def doRead(self): data = os.read(self.fifo, 1024) if data: self.protocol.dataReceived(data) else: self.stopReading() self.connectionLost(CONNECTION_DONE) def fileno(self): return self.fifo And here's a brief program that uses it: import sys from twisted.internet import reactor from twisted.internet.protocol import Protocol class PrintingProtocol(Protocol): def dataReceived(self, data): print 'data', repr(data) def connectionMade(self): print 'hi' def connectionLost(self, reason): print 'bye', reason fifd = FIFODescriptor(reactor, sys.argv[1], PrintingProtocol()) fifd.startReading() reactor.run() I tested this by doing 'mkfifo x; python fifo.py x', and in another terminal, 'cat > x'. It should be pretty self-explanitory.
Thanks! That makes sense. I appreciate the detailed example. Best regards. Rodney -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com]On Behalf Of glyph@divmod.com Sent: Friday, August 29, 2008 5:21 PM To: Twisted general discussion Subject: Re: [Twisted-Python] How does the pollreactor work? On 03:35 pm, rlott@evertz.com wrote:
I have been trying to figure out how to use the twisted.internet.pollreactor so that I can use it to monitor a Linux named PIPE (i.e. a FIFO created using mkfifo()). I tried without success to Google some examples but I couldn't find anything.
This doesn't really have anything to do with pollreactor. Any reactor that implements IReactorFDSet can monitor a pipe. It was easier for me to implement this than to explain what to do, so here's the code for monitoring a named pipe for read, given its name. I'm pretty sure it's not 100% correct, but if you want to read about handling I/O events from fifos on your platform, you are welcome to do so. import os from twisted.internet.abstract import FileDescriptor from twisted.internet.main import CONNECTION_DONE class FIFODescriptor(FileDescriptor): def __init__(self, reactor, fifoname, protocol): FileDescriptor.__init__(self, reactor) self.fifo = os.open(fifoname, os.O_NONBLOCK | os.O_RDONLY) self.protocol = protocol self.protocol.makeConnection(self) def connectionLost(self, reason): self.protocol.connectionLost(reason) def doRead(self): data = os.read(self.fifo, 1024) if data: self.protocol.dataReceived(data) else: self.stopReading() self.connectionLost(CONNECTION_DONE) def fileno(self): return self.fifo And here's a brief program that uses it: import sys from twisted.internet import reactor from twisted.internet.protocol import Protocol class PrintingProtocol(Protocol): def dataReceived(self, data): print 'data', repr(data) def connectionMade(self): print 'hi' def connectionLost(self, reason): print 'bye', reason fifd = FIFODescriptor(reactor, sys.argv[1], PrintingProtocol()) fifd.startReading() reactor.run() I tested this by doing 'mkfifo x; python fifo.py x', and in another terminal, 'cat > x'. It should be pretty self-explanitory. _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
glyph@divmod.com
-
Rodney Lott