<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><span style="font-size: 12pt; ">Message: 1</span><br></div><div style="font-family: 'times new roman', 'new york', times, serif; "><div style="font-size: 12pt; font-family: 'times new roman', 'new york', times, serif; ">Date: Thu, 17 May 2012 00:58:57 +0200<br>From: Louis &lt;<a ymailto="mailto:spalax@gresille.org" href="mailto:spalax@gresille.org">spalax@gresille.org</a>&gt;<br>Subject: [Twisted-Python] Synchronous calls using Twisted?<br>To: <a ymailto="mailto:twisted-python@twistedmatrix.com" href="mailto:twisted-python@twistedmatrix.com">twisted-python@twistedmatrix.com</a><br>Message-ID: &lt;<a ymailto="mailto:4FB43131.2030805@gresille.org" href="mailto:4FB43131.2030805@gresille.org">4FB43131.2030805@gresille.org</a>&gt;<br>Content-Type: text/plain; charset=ISO-8859-1<br><br>&gt; &nbsp;Hello,<br>&gt; &nbsp;I am writing
 an application, which architecture is (I hope my<br>&gt;beautiful drawing is not messed up):<br><br>&gt; &nbsp;+-------------+&nbsp;  +------------+&nbsp;  +-----------+<br>&gt; &nbsp;| Core Server |---| Web Server |---| Web client|<br>&gt; &nbsp;+-------------+&nbsp;  +------------+&nbsp;  +-----------+<br>&gt;...</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br>&gt; &nbsp;Do you think Twisted is the right tool for my use case, or am I doing<br>&gt;an ugly hack to do what I want, which means I should rather use another<br>&gt;tool?<br><br> </div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861">I am not sure why inline generators won't work?</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div
 style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861">Over the years, I use Stackless Python with Twisted. I use Twisted for many of the reasons you describe. For myself there is another dimension: I find that a lot of the code I write are orchestrations - that is in order to compute something, I have to make a few network calls sequentially.&nbsp;</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861">&nbsp;I use a technique that Christopher Armstrong (what happened to him?) called a blockOn. For the sake of being academic (roll your eyes here), &nbsp;this is an example of an obscure design pattern called "Half-sync/Half-async."</div><div style="font-family: 'times new
 roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861">Essentially one does the following:</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div class="yui_3_2_0_145_133728002853861"><span style="font-family: verdana, geneva, helvetica, arial, sans-serif; font-size: 13px; ">def&nbsp;<b class="highlight" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 102); background-position: initial initial; background-repeat: initial initial; ">blockOn</b>(deferred):&nbsp;</span><br style="font-family: verdana, geneva, helvetica, arial, sans-serif; font-size: 13px; "><span style="font-family: verdana,
 geneva, helvetica, arial, sans-serif; font-size: 13px; ">&nbsp; &nbsp; ch =&nbsp;<b class="highlight" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 102); background-position: initial initial; background-repeat: initial initial; ">stackless</b>.channel()&nbsp;</span><br style="font-family: verdana, geneva, helvetica, arial, sans-serif; font-size: 13px; "><span style="font-family: verdana, geneva, helvetica, arial, sans-serif; font-size: 13px; ">&nbsp; &nbsp; def cb(result):&nbsp;</span><br style="font-family: verdana, geneva, helvetica, arial, sans-serif; font-size: 13px; "><span style="font-family: verdana, geneva, helvetica, arial, sans-serif; font-size: 13px; ">&nbsp; &nbsp; &nbsp; &nbsp; ch.send(result)&nbsp;</span><br style="font-family: verdana, geneva, helvetica, arial, sans-serif; font-size: 13px; "><span style="font-family: verdana, geneva,
 helvetica, arial, sans-serif; font-size: 13px; ">&nbsp; &nbsp; deferred.addBoth(cb)&nbsp;</span><br style="font-family: verdana, geneva, helvetica, arial, sans-serif; font-size: 13px; "><span style="font-family: verdana, geneva, helvetica, arial, sans-serif; font-size: 13px; ">&nbsp; &nbsp; return ch.receive()&nbsp;</span><br style="font-family: verdana, geneva, helvetica, arial, sans-serif; font-size: 13px; "></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861">Of course, there is a bit more, like the Twisted reactor is running in its own tasklet. But this has the effect of nicely allowing one to use Twisted in a synchronous fashion. If you don't feel like installing Stackless Python, then you can use stackless.py (not the new version that
 uses continuations) with the greenlet package.</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861">Here is a link to a complete example</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><a href="http://andrewfr.wordpress.com/2011/11/30/the-santa-claus-problem-with-join-patterns/">http://andrewfr.wordpress.com/2011/11/30/the-santa-claus-problem-with-join-patterns/</a><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div
 style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861">Cheers,</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861">Andrew</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br></div> </div>  </div></body></html>