[Twisted-Python] Perspective broker over standard IO. How?

Maxim Lacrima lacrima.maxim at gmail.com
Fri Feb 8 09:28:05 EST 2013


Hi Itamar,

Thank you for your hints. I got it working:

----
# client.py
from twisted.internet.endpoints import StandardIOEndpoint
from twisted.internet import reactor
from twisted.spread import pb

class Foo(pb.Root):
    def remote_do_smth(self):
        return 'hello!'

def main():
    endpoint = StandardIOEndpoint(reactor)
    f = pb.PBServerFactory(Foo())
    endpoint.listen(f)
    reactor.run()

if __name__ == '__main__':
    main()

----

----
# main.py
import sys
from twisted.spread import pb
from twisted.internet import reactor, protocol
from twisted.python import util

def do_something(obj):
    d = obj.callRemote("do_smth")
    return d

class FooProcessProtocol(protocol.ProcessProtocol):

    def __init__(self, PBFactory):
        self.broker = PBFactory.buildProtocol(('foo',))

    def connectionMade(self):
        self.broker.makeConnection(self.transport)

    def outReceived(self, data):
        self.broker.dataReceived(data)

    def errReceived(self, data):
        print data


def main():
    exe = sys.executable
    args = [exe, '~/misc/client.py']

    factory = pb.PBClientFactory()
    reactor.spawnProcess(FooProcessProtocol(factory), exe, args)

    d = factory.getRootObject()
    d.addCallback(do_something)
    d.addCallback(util.println)
    d.addCallback(lambda _: reactor.stop())

    reactor.run()

if __name__ == '__main__':
    main()

----

$ python main.py
hello!

It works. I just wonder if I there are some edge cases that are not handled
by this implementation.

Thank you.


On 8 February 2013 14:46, Itamar Turner-Trauring <itamar at futurefoundries.com
> wrote:

> Eventually this will be easier, BTW, once this ticket is merged:
> https://twistedmatrix.com/trac/ticket/4696
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>


-- 
Regards,
Maxim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20130208/648befbb/attachment.htm 


More information about the Twisted-Python mailing list