Outils pour utilisateurs

Outils du site


using_perspective_broker

Ceci est une ancienne révision du document !


Using Perspective Broker

Niveau Pro et intergalactique !

Traduction Google de Using Perspective Broker améliorée par un humain ayant fait intergalactique en 1ère langue !

Exemple simple

Passez votre chemin, cela revient à expliquer la factorisation à un élève de 5ème, en lui parlant de nombres complexes et de cosinus hyperbolique: pourquoi utiliser des “lambda” qui ne sont pas pythoniques ???? m(

Exemple 1

La méthode remote_three() du serveur est appelée par le client avec

obj2.callRemote("three", 12)

De manière générale, sur le client, on peut appeler remote_toto(un_argument) avec

truc.callRemote("toto", un_argument)

Cela appelle la méthode à distance et cela transfert un_argument du client au serveur!
La taille maxi de l'argument est décrit ici: 640 * 1024 bytes ou entier de +ou- 2448
Cette façon de procéder est une des 3 façons possibles(saveurs).

pb1server.py
from twisted.spread import pb
class Two(pb.Referenceable):
    def remote_three(self, arg):
        """Appelé par le callback three sur le client
        obj2.callRemote("three", 12)
        """
        print("Two.three was given", arg)
class One(pb.Root):
    def remote_getTwo(self):
        """Appelé par le callback getTwo sur le client
        def2 = obj1.callRemote("getTwo")
        """
        two = Two()
        print("returning a Two called", two)
        return two
from twisted.internet import reactor
reactor.listenTCP(8800, pb.PBServerFactory(One()))
reactor.run()
pb1client.py
from twisted.spread import pb
from twisted.internet import reactor
def main():
    factory = pb.PBClientFactory()
    reactor.connectTCP("localhost", 8800, factory)
    def1 = factory.getRootObject()
    def1.addCallbacks(got_obj1)
    reactor.run()
def got_obj1(obj1):
    print("got first object:", obj1)
    print("asking it to getTwo")
    def2 = obj1.callRemote("getTwo")
    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()

Explications

Exemple 2

pb2server.py
from twisted.internet import reactor
from twisted.spread import pb
class Two(pb.Referenceable):
    def remote_print(self, arg):
        print("class Two dans serveur: two.print was given", arg)
 
class One(pb.Root):
    def __init__(self, two):
        # pb.Root.__init__(self)   # pb.Root doesn't implement __init__
        self.two = two
    def remote_getTwo(self):
        """Méthode appelée par getTwo chez le client avec
        self.oneRef.callRemote("getTwo").addCallback(self.step2)"""
        print("One.getTwo(), returning my two called", self.two)
        return self.two
    def remote_checkTwo(self, new_two):
        """Méthode appelée par checkTwo chez le client
        self.oneRef.callRemote("checkTwo", two)"""
        print("appelé par quoi ?")
        print("One.checkTwo(): comparing my two", self.two)
        print("One.checkTwo(): against your two", new_two)
        if self.two == new_two:
            print("One.checkTwo(): our twos are the same")
two = Two()
root_obj = One(two)
reactor.listenTCP(8800, pb.PBServerFactory(root_obj))
reactor.run()
pb2client.py
from twisted.internet import reactor
from twisted.spread import pb
def main():
    foo = Foo()
    factory = pb.PBClientFactory()
    reactor.connectTCP("localhost", 8800, factory)
    factory.getRootObject().addCallback(foo.step1)
    reactor.run()
 
class Foo:
    """Garder les globals autour commence à devenir moche, nous utilisons donc 
    une classe simple à la place. Au lieu d'accrocher une fonction à la suivante, 
    nous accrochons une méthode à la suivante."""
    def __init__(self):
        self.oneRef = None
    def step1(self, obj):
        print("got one object:", obj)
        self.oneRef = obj
        print("asking it to getTwo")
        self.oneRef.callRemote("getTwo").addCallback(self.step2)
    def step2(self, two):
        print("got two object:", two)
        print("giving it back to one")
        print("one is", self.oneRef)
        self.oneRef.callRemote("checkTwo", two)
main()

Explications

Exemple 3

Explications

using_perspective_broker.1669712269.txt.gz · Dernière modification : de serge