Txnamedpipes: using Windows namedpipes with twisted
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.
Txnamedpipes (lp:txnamedpipes) 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.
from txnamedpipes.reactor import install install() from twisted.spread import pb from twisted.internet import reactor class Echoer(pb.Root): def remote_echo(self, st): print 'echoing:', st return st if __name__ == '__main__': reactor.listenPipe('\\.\pipe\test_pipe', pb.PBServerFactory(Echoer())) reactor.run()
from txnamedpipes.reactor import install install() from twisted.spread import pb from twisted.internet import reactor from twisted.python import util factory = pb.PBClientFactory() reactor.connectPipe('\\.\pipe\test_pipe', factory) d = factory.getRootObject() d.addCallback(lambda object: object.callRemote("echo", "hello network")) d.addCallback(lambda echo: 'server echoed: '+echo) d.addErrback(lambda reason: 'error: '+str(reason.value)) d.addCallback(util.println) d.addCallback(lambda _: reactor.stop()) reactor.run()
The code has the MIT license and we hope that other people find it useful.