<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 <<a ymailto="mailto:spalax@gresille.org" href="mailto:spalax@gresille.org">spalax@gresille.org</a>><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: <<a ymailto="mailto:4FB43131.2030805@gresille.org" href="mailto:4FB43131.2030805@gresille.org">4FB43131.2030805@gresille.org</a>><br>Content-Type: text/plain; charset=ISO-8859-1<br><br>> Hello,<br>> I am writing
an application, which architecture is (I hope my<br>>beautiful drawing is not messed up):<br><br>> +-------------+ +------------+ +-----------+<br>> | Core Server |---| Web Server |---| Web client|<br>> +-------------+ +------------+ +-----------+<br>>...</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; " class="yui_3_2_0_145_133728002853861"><br>> Do you think Twisted is the right tool for my use case, or am I doing<br>>an ugly hack to do what I want, which means I should rather use another<br>>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. </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"> I use a technique that Christopher Armstrong (what happened to him?) called a blockOn. For the sake of being academic (roll your eyes here), 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 <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): </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; "> ch = <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() </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; "> def cb(result): </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; "> ch.send(result) </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; "> deferred.addBoth(cb) </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; "> return ch.receive() </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>