Hello!<div><br></div><div>I am very new to Twisted and I have some problems using stdin.StandardIO. Here is the code:</div><div><br></div><div># item.py file</div><div><div>from twisted.internet import reactor, stdio</div>
<div>from twisted.protocols import basic</div><div>from storage import Item</div><div><br></div><div>class EchoItemProtocol(basic.LineReceiver):</div><div> delimiter = '\n'</div><div><br></div><div> def lineReceived(self, item_id):</div>
<div> # Item.get returns a deferred</div><div> d = Item.get(item_id)</div><div> def writeResponse(item):</div><div> self.transport.write(str(item) + '\n')</div><div> d.addCallback(writeResponse)</div>
<div><br></div><div>def main():</div><div> stdio.StandardIO(EchoItemProtocol())</div><div> reactor.run()</div><div><br></div><div>if __name__ == '__main__':</div><div> main()</div><div><br></div><div><br>
</div><div>So, when I run the file it works:</div><div><div>$ ./item.py</div><div>test_item_id</div><div><storage.Item object at 0x2b15910></div></div><div><br></div><div>I want to be able to redirect output of some process into my script. But this doesn't work:</div>
<div>$ echo test_item_id | ./item.py</div><div><br></div><div>It doesn't produce any output.</div><div><br></div><div>But if i replace `self.transport.write(str(item) + '\n')` with just `print str(item)` it suddenly works:</div>
<div><div>$ echo test_item_id | ./item.py</div><div><storage.Item object at 0x18ac650></div></div><div><br></div><div>So my conclusions are:</div><div>1. When I write to stdin of my script from keyboard then `self.transport.write()` works as expected.</div>
<div>2. When I use pipes or redirection to my script `self.transport.write()` doesn't produce any output.</div><div>3. If I replace self.transport.write() with print statements it works.</div><div><br></div><div>Why is this strange behavior?</div>
<div><br></div>-- <br>with regards,<br>Maxim<br>
</div>