zoukankan      html  css  js  c++  java
  • Firefly distributed模块的原理与twisted中PB远程调用协议

    这些天断断续续在看Firefly, 看了一下distributed模块的设计,其实就是使用的twisted.spread.pb
    觉得以后要是想用Firefly有必要了解一下twisted, 所以在网上查了一下资料,更好的资料莫过于官方资料了,官方的例子挺多的,我挑了这一个例子:


    这里给个官方的例子:
    server:


    from twisted.spread import pb

    # 这里使用Referenceable是可以通过远程调用获得此对象
    class Two(pb.Referenceable):
        # remote_前缀是twisted要求的规范, Server中提供的方法必须以remote_开头
        def remote_three(self, arg):
            print "Two.three was given", arg

    # Factory中的Root对象必须继承自pb.Referenceable
    # pb.Root就继承自pb.Referenceable
    class One(pb.Root):
        def remote_getTwo(self):
            two = Two()
            print "returning a Two called", two
            return two

    from twisted.internet import reactor
    reactor.listenTCP(8800, pb.PBServerFactory(One()))
    reactor.run()


    client:

    from twisted.spread import pb
    from twisted.internet import reactor

    def main():
        factory = pb.PBClientFactory()
        reactor.connectTCP("localhost", 8800, factory)
         # getRootObject返回的是一个Deferred实例
        def1 = factory.getRootObject()
         # 添加callback处理函数got_obj1,如果出错则执行err_obj1
         # 
        def1.addCallbacks(got_obj1, err_obj1)
        reactor.run()

    def err_obj1(reason):
        print "error getting first object", reason
        reactor.stop()

    def got_obj1(obj1):
        print "got first object:", obj1
        print "asking it to getTwo"
         # Deferred通过callRemote调用远程对象,返回的还是个Deferred
        def2 = obj1.callRemote("getTwo")
         # 给Deferred添加返回处理函数
        def2.addCallbacks(got_obj2)

    def got_obj2(obj2):
        print "got second object:", obj2
        print "telling it to do three(12)"
        obj2.callRemote("three", 12)

    main()

    Twisted针对Server和Client分别提供了pb.PBServerFactory和pb.PBClientFactory供用户使用.所以我们很方便就可以建立通讯。这里我们不论是我们getRootObject还是callRemote返回的都是Deferred对象, twisted使用Deferred对象管理callback系列。

    远程方法调用除了可以返回和传递字符串、词典、列表等简单对象外,还可以传递pb.Referenceable对象。上面的例了中callRemote("getTwo")调用remote_getTwo,返回的是Two的实例,是一个pb.Referenceable对象。接着可以在callback中再次调用callRemote("three")调用remote_three。

    如果需要的户客端获取返回的数据,比如返回的列表则可以这样做:
    class Two(pb.Referenceable):
        def remote_three(self, arg):
            print "Two.three was given", arg

            return [1,2,3]
    这里还是用上面的代码,只是在three中添加一个返回,为了获取最后返回的这个列表,则需要为callRemote("three", 12)添加一个callback:
    def got_obj2(obj2):
        print "got second object:", obj2
        print "telling it to do three(12)"
        obj2.callRemote("three", 12).addCallbacks(test)


    def test(obj):
         print obj

    些处添加了一个test函数处理callRemote("three", 12)返回的数据,传递的obj就是返回的[1,2,3]。

    大部份都写在注释里了,写得很乱,但也是我学twisted的一点心得 ^_^

    参考:
    cnblogs.com/zhangjing0502/archive/2012/05/16/2504415.html

  • 相关阅读:
    iNeuOS工业互联平台,WEB组态(iNeuView)集成rtmp和websocket视频元件,支持海康、大华等摄像头实时显示视频
    MyBatis之TypeHandler用法
    视频监控安防专网事件预警运维系统开发方案
    Qmgo 开源了! 更好用的 Go 语言 MongoDB driver
    前端使用axios传递数组后端使用List接收
    jdk1.6手册java8中英手册java9英文手册.chm(下载)
    自动化运维工具Ansible (一)
    初级模拟电路:4-11 混合π模型
    简单体验一个高性能,简单,轻量的ORM库- Dapper (无依赖其它库,非常方便高效)
    Python计算大文件行数方法及性能比较
  • 原文地址:https://www.cnblogs.com/zhepama/p/3536022.html
Copyright © 2011-2022 走看看