<br><br><div class="gmail_quote">On Wed, Aug 5, 2009 at 4:17 PM,  <span dir="ltr">&lt;<a href="mailto:exarkun@twistedmatrix.com">exarkun@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 class="im">On 09:33 am, <a href="mailto:johnaherne@rocs.co.uk">johnaherne@rocs.co.uk</a> wrote:<br>
&gt;On Wed, Aug 5, 2009 at 12:14 AM, Johann Borck<br>
&gt;&lt;<a href="mailto:johann.borck@densedata.com">johann.borck@densedata.com</a>&gt;wrote:<br>
&gt;&gt;<br>
</div>&gt;[snip]<br>
<div class="im">&gt;<br>
&gt;Sendline is not blocking so as you say we can avoid the use of<br>
&gt;deferreds and<br>
&gt;continue to use sendline directly.<br>
<br>
</div>LineReceiver.sendLine is not blocking, correct.  However, your statement<br>
implies that if it were blocking, you could use Deferreds to address<br>
this<br>
problem.  This is incorrect.  Deferreds do not make blocking APIs into<br>
non-blocking APIs.<br>
&gt;[snip]<br>
<div class="im">&gt;<br>
&gt;Our option 3 using defertothread does use sendline from the thread.<br>
&gt;Your<br>
&gt;response implies that is OK since you say defertothread is threadsafe.<br>
&gt;Did<br>
&gt;you really mean that.<br>
<br>
</div>deferToThread is not thread-safe: you may only call it from the reactor<br>
thread<br>
(the thread in which you called reactor.run).<br>
<br>
Since deferToThread runs the function you pass to it in a non-reactor<br>
thread,<br>
you may not use any non-thread-safe Twisted APIs in the function you<br>
pass to<br>
<div class="im">it.<br>
&gt;<br>
&gt;Once again thanks for a very good response. That has cleared up a lot<br>
&gt;of<br>
&gt;confusion.<br>
&gt;<br>
&gt;I suppose it would help if there was a paragraph at the start of the<br>
&gt;twisted<br>
&gt;documentation detailing what you have just said. So when they start on<br>
&gt;deferreds you have some sort of context in which to interpret what is<br>
&gt;being<br>
&gt;said<br>
<br>
</div>A significant effort is presently underway to improve the documentation<br>
about<br>
Deferreds.  Any specific feedback you have about it would be much<br>
appreciated.<br>
:)<br>
<br>
Jean-Paul<br>
<div><div></div><div class="h5"><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>
</div></div></blockquote></div><br><div>Jean-Paul</div><div><br></div><div>Thanks for the clarifications.</div><div><br></div><div>I assume what I meant by blocking was I would have to put a function into deferToThread and add some callbacks to return the result. And not directly calling sendline from the thread. This is what Jarrod suggested in his reply. If I&#39;m wrong, please say so.</div>
<div><br></div><div>I have read the thread regarding deferreds with interest, but did not feel I knew enough to contribute.</div><div><br></div><div>I do feel qualified to ask some very daft questions. Unfortunately, I don&#39;t see too many daft questions being asked in the list. I reckon if I need to know the answer to them then some other people probably do as well but don&#39;t put themselves forward. I see it as a way to document information that is difficult to come by. And I really do appreciate the very good answers I get</div>
<div><br></div><div>However, after this little foray, I probably feel able to comment:--</div><div><br></div><div>The concept of deferreds is very simple. Everyone understands the concept - even I do. The issue is how and why and where you should use them in twisted.</div>
<div><br></div><div>Some Basic getting Started Points.</div><div><br></div><div>1. For simple network activity do not use deferreds. They are not necessary. You can get a lot done without deferreds. And you don&#39;t know how to use them yet. The reactor and the select will process the outgoing and incoming buffers without blocking. Anyone familiar with networking and select will already understand this. Anyone not familiar will not realise it and needs to be made aware of how the select works.</div>
<div><br></div><div>2. If you have blocking code - please define blocking :), then first think about  putting it into deferToThread with appropriate callbacks and return the deferred. As suggested by Jarrod in his response.</div>
<div><br></div><div>3. John Goerzen in his Apress book Python Network Fundamentals has a very simple chat server example. With a few comments for the uninitiated, this would be a good starter. Possibly I could ask for permission to include it in some twisted HOWTO documentation for beginners with suitable copyright recognition.</div>
<div><br></div><div>4. With these few points as starters, maybe more people will be encouraged to get started with twisted. And if you know you can ignore deferreds until later you will find twisted is very simple to use and get some good results with little effort. </div>
<div><br></div><div>5. The emphasis on how deferreds work probably ought to be counterbalanced by some insight into how and why and where you would use them. For example, if you have a text file of 10000 lines you need to read in and summarize, presumably you would run this with deferToThread(+other options) and get the result via the callback. If someone has a better example please let me know.</div>
<div><br></div><div>6. Blocking code is always put into a thread or like, and a deferred callback or errback used to return the result or failure of the blocking code from the thread. See jarrod&#39;s response above</div>
<div><br></div><div><br></div><div>You asked :)</div><div>I&#39;m giving</div><div><br></div><div>I may be completely off track in what I have said above. And I would not want anyone to fall upon this mail and think it represents the gospel truth.</div>
<div><br></div><div>Thank you for the response..</div><div><br></div><div>John Aherne</div>