[Twisted-Python] How does the pollreactor work?

Rodney Lott rlott at evertz.com
Tue Sep 2 06:43:38 MDT 2008

Thanks!  That makes sense.  I appreciate the detailed example.

Best regards.


-----Original Message-----
From: twisted-python-bounces at twistedmatrix.com
[mailto:twisted-python-bounces at twistedmatrix.com]On Behalf Of
glyph at 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 at 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 

    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

        def connectionLost(self, reason):

        def doRead(self):
            data = os.read(self.fifo, 1024)
            if data:

        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())

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 at twistedmatrix.com

More information about the Twisted-Python mailing list