Hi everyone!<div><br></div><div>Thanks for the help! I used an attached example and after implementing <span style="background-color:rgb(255,255,255);color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">IHalfCloseableProtocol</span> it works! Thanks a lot!</div>
<div><br></div><div>Still, Twisted seems hard to me, probably because I don&#39;t quite understand how to use and combine Twisted&#39;s interfaces.</div><div>With your help I can now connect my program to another process using pipe. But, for example, I want to be able to supply data to my program not only via stdin, but also as command line arguments. I.e. instead of</div>
<div>$ echo foo | ./item.py</div><div><br></div><div>I want to do:</div><div>$ ./item.py foo</div><div><br></div><div>My feeling here is that I can reuse the same protocol (EchoItemProtocol) here, but now instead of StandardIO I need my own factory (or transport, or whatever it is named) class that knows how to read command line args and talk the same protocol. Sure I can read arguments using sys.argv[1:], but what do I do next to properly pass that data to the protocol? Does my class have to implement ITransport or IProducer etc. to talk EchoItemProtocol similar to StandardIO?</div>
<div>I hope my question is clear.</div><div><br></div><div>Thanks in advance.</div><div><br><div class="gmail_quote">On 10 August 2012 08:18, Glyph <span dir="ltr">&lt;<a href="mailto:glyph@twistedmatrix.com" target="_blank">glyph@twistedmatrix.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div class="im"><br><div><div>On Aug 9, 2012, at 7:39 PM, Andrew Bennetts &lt;<a href="mailto:andrew@bemusement.org" target="_blank">andrew@bemusement.org</a>&gt; wrote:</div>
<br><blockquote type="cite">Maxim Lacrima wrote:<br>[…]<br><blockquote type="cite">So my conclusions are:<br>1. When I write to stdin of my script from keyboard then<br>`self.transport.write()` works as expected.<br>2. When I use pipes or redirection to my script `self.transport.write()`<br>
doesn&#39;t produce any output.<br>3. If I replace self.transport.write() with print statements it works.<br></blockquote><br>Buffering, perhaps?<br><br>As a quick test, try adjusting your callback to loseConnection on the transport<br>
after to the write, that should flush the buffers.  If buffering turns out to<br>be your problem, and you really do expect your pipes to be at most<br>line-buffered, you&#39;ll need to arrange for the buffering on the stdin and stdout<br>
file descriptors to be changed.  I don&#39;t think Twisted has that builtin, so you<br>may need to do that before creating the StandardIO object using the regular<br>Python APIs.<br></blockquote></div><br></div><div>It&#39;s not this kind of buffering problem.  I was actually able to reproduce it by replacing &quot;Item.get()&quot; with a deferLater.  (In the future, please perform these types of replacements yourself before posting your examples; it&#39;s much easier to help out with complete, runnable programs.)</div>
<div><br></div><div>The issue is that twisted.internet.stdio considers your protocol to only know about one kind of connection loss: i.e. that your entire transport has gone away.  So, when the input is closed, the output file descriptor is immediately closed as well.</div>
<div><br></div><div>Unfortunately - and I think this is a bug in Twisted - the input connection being lost causes the output connection to be immediately and somewhat aggressively closed.  The difference between &quot;echo&quot; and typing interactively is that you send &quot;test_item_id\n&quot;, but echo sends &quot;test_item_id\n&quot; *EOF*.</div>
<div><br></div><div>Luckily you can work around this, by explicitly asking Twisted for notifications about half of the connection being closed.  You do this by implementing IHalfCloseableProtocol.</div><div><br></div><div>
Although Twisted should be better about how it handles shutting down stdout, this nuance <i>does</i> reveal some logic that your code is missing: once your input is done, you don&#39;t have any concept of waiting for your <i>output </i>to be done before shutting everything down.  So, if you add both the IHalfCloseableProtocol implements declaration to receive the relevant notifications, and some logic to keep track of when all the output has been written, you can get exactly the behavior that I assume you want.</div>
<div><br></div><div>I&#39;ve attached an example that does all of these things with a simple timer, since this is something that Twisted probably needs to document a bit better.</div><div><br></div><div></div></div><br><div style="word-wrap:break-word">
<div></div><div><br></div><div>Cheers,</div><div><br></div><div>-glyph</div></div><br>_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>with regards,<br>Maxim<br>
</div>