<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">half async/half sync is not as obscure as you may think:<div><br></div><div><span class="f" style="color: rgb(102, 102, 102); font-family: arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); font-size: small; "></span><div><span style="color: rgb(0, 153, 51); font-family: arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); font-size: small; display: inline !important; float: none; "><a href="http://www.cs.wustl.edu/~">www.cs.wustl.edu/~</a></span><b style="color: rgb(0, 153, 51); font-family: arial, sans-serif; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); font-size: small; ">schmidt</b><span style="color: rgb(0, 153, 51); font-family: arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); font-size: small; display: inline !important; float: none; ">/PDF/PLoP-95.pdf</span></div><div><font class="Apple-style-span" color="#009933" face="arial, sans-serif" size="2"><span class="Apple-style-span" style="line-height: 15px;"><br></span></font><div><div>On May 17, 2012, at 12:06:04PM, Andrew Francis wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div><div style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; position: static; z-index: auto; "><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></div>_______________________________________________<br>Twisted-Python mailing list<br><a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python<br></blockquote></div><br></div></div></body></html>