Canonical Voiceshttp://voices.canonical.com2011-06-27T11:21:56ZManuel de la Pena: Txnamedpipes: using Windows namedpipes with twisted2011-06-27T11:21:56Zmandelnospam@nospam.comhttp://www.themacaque.com/?p=893<p>At Ubuntu One we required to be able to use named pipes on windows for IPC. This is a ver normal process in multi-process applications like the one we are going to provide, but in our case we had a twist, we are using twisted. As some of you may know there is not default reactor that would allow you to write a protocol in twisted and allows to use named pipes as the transport of the protocol. Well this was until very recently.</p>
<p>Txnamedpipes (<a href="https://launchpad.net/txnamedpipes">lp:txnamedpipes</a>) is a project that provides a ICOP based reactor that allows to use namedpipes for the transport of your protocol. At the moment we are confident that the implementation would allow you to use spred.pb or a custom protocol on twisted 10 and later on Windows 7 (we have been able to find a number of issues on Windows XP). The following is a small example of a spread.pb service and client that uses a named pipe for communication.</p>
<div class="wp_syntax"><div class="code"><pre class="python"><span>from</span> txnamedpipes.<span>reactor</span> <span>import</span> install
install<span>&#40;</span><span>&#41;</span>
<span>from</span> twisted.<span>spread</span> <span>import</span> pb
<span>from</span> twisted.<span>internet</span> <span>import</span> reactor
&nbsp;
<span>class</span> Echoer<span>&#40;</span>pb.<span>Root</span><span>&#41;</span>:
<span>def</span> remote_echo<span>&#40;</span><span>self</span>, st<span>&#41;</span>:
<span>print</span> <span>'echoing:'</span>, st
<span>return</span> st
&nbsp;
<span>if</span> __name__ == <span>'__main__'</span>:
reactor.<span>listenPipe</span><span>&#40;</span><span>'<span>\\</span><span>\\</span>.<span>\\</span>pipe<span>\\</span>test_pipe'</span>,
pb.<span>PBServerFactory</span><span>&#40;</span>Echoer<span>&#40;</span><span>&#41;</span><span>&#41;</span><span>&#41;</span>
reactor.<span>run</span><span>&#40;</span><span>&#41;</span></pre></div></div>
<div class="wp_syntax"><div class="code"><pre class="python"><span>from</span> txnamedpipes.<span>reactor</span> <span>import</span> install
install<span>&#40;</span><span>&#41;</span>
<span>from</span> twisted.<span>spread</span> <span>import</span> pb
<span>from</span> twisted.<span>internet</span> <span>import</span> reactor
<span>from</span> twisted.<span>python</span> <span>import</span> util
&nbsp;
factory = pb.<span>PBClientFactory</span><span>&#40;</span><span>&#41;</span>
reactor.<span>connectPipe</span><span>&#40;</span><span>'<span>\\</span><span>\\</span>.<span>\\</span>pipe<span>\\</span>test_pipe'</span>, factory<span>&#41;</span>
d = factory.<span>getRootObject</span><span>&#40;</span><span>&#41;</span>
d.<span>addCallback</span><span>&#40;</span><span>lambda</span> <span>object</span>: <span>object</span>.<span>callRemote</span><span>&#40;</span><span>&quot;echo&quot;</span>,
<span>&quot;hello network&quot;</span><span>&#41;</span><span>&#41;</span>
d.<span>addCallback</span><span>&#40;</span><span>lambda</span> echo: <span>'server echoed: '</span>+echo<span>&#41;</span>
d.<span>addErrback</span><span>&#40;</span><span>lambda</span> reason: <span>'error: '</span>+<span>str</span><span>&#40;</span>reason.<span>value</span><span>&#41;</span><span>&#41;</span>
d.<span>addCallback</span><span>&#40;</span>util.<span>println</span><span>&#41;</span>
d.<span>addCallback</span><span>&#40;</span><span>lambda</span> _: reactor.<span>stop</span><span>&#40;</span><span>&#41;</span><span>&#41;</span>
reactor.<span>run</span><span>&#40;</span><span>&#41;</span></pre></div></div>
</p>
<p>The code has the MIT license and we hope that other people find it useful.</p>