[Twisted-Python] Using StandardIO and pipes.

Maxim Lacrima lacrima.maxim at gmail.com
Thu Aug 9 03:46:18 EDT 2012


Hello!

I am very new to Twisted and I have some problems using stdin.StandardIO.
Here is the code:

# item.py file
from twisted.internet import reactor, stdio
from twisted.protocols import basic
from storage import Item

class EchoItemProtocol(basic.LineReceiver):
    delimiter = '\n'

    def lineReceived(self, item_id):
        # Item.get returns a deferred
        d = Item.get(item_id)
        def writeResponse(item):
            self.transport.write(str(item) + '\n')
        d.addCallback(writeResponse)

def main():
    stdio.StandardIO(EchoItemProtocol())
    reactor.run()

if __name__ == '__main__':
    main()


So, when I run the file it works:
$ ./item.py
test_item_id
<storage.Item object at 0x2b15910>

I want to be able to redirect output of some process into my script. But
this doesn't work:
$ echo test_item_id | ./item.py

It doesn't produce any output.

But if i replace `self.transport.write(str(item) + '\n')` with just
`print str(item)` it suddenly works:
$ echo test_item_id | ./item.py
<storage.Item object at 0x18ac650>

So my conclusions are:
1. When I write to stdin of my script from keyboard then
`self.transport.write()` works as expected.
2. When I use pipes or redirection to my script `self.transport.write()`
doesn't produce any output.
3. If I replace self.transport.write() with print statements it works.

Why is this strange behavior?

-- 
with regards,
Maxim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20120809/fbdd83fc/attachment.htm 


More information about the Twisted-Python mailing list