Thriftpy : Implémentation Python du protocole Apache Thrift pour RPC

Thriftpy est une implémentation pure en Python du protocole Apache Thrift, conçue pour la construction de cadres SOA internes. Ce projet open-source est disponible sur GitHub : https://github.com/eleme/thriftpy.

Types de données

  • bool : type booléen (true ou false), occupant un octet.
  • byte : octet signé.
  • i16 : entier signé 16 bits.
  • i32 : entier signé 32 bits.
  • i64 : entier signé 64 bits.
  • double : nombre flottant 64 bits.
  • string : chaîne de caractères à encodage inconnu ou binaire.

Exemple de définition Thrift

Fichier alarme.thrift :

service ServiceAlarme {
    string declencher();
}

Fichier echo.thrift :

service ServiceEcho {
    string appeler();
}

service ServiceEchoAvecArgs {
    string appeler(1:string hote, 2:i32 port);
}

service ServiceRepos {
    oneway void reposer(1:i32 secondes);
}

Serveur Thrift en Python

Voici un exemple de code serveur utilisant Thriftpy :

import thriftpy
from thriftpy.rpc import make_server
from thriftpy.thrift import TProcessor, TMultiplexedProcessor
from thriftpy.transport import TBufferedTransportFactory, TServerSocket
from thriftpy.protocol import TBinaryProtocolFactory
from thriftpy.server import TThreadedServer
import time

class GestionnaireEcho(object):
    def appeler(self):
        return "Écho réussi"

    def appeler_avec_args(self, hote, port):
        print(f"{hote}:{port}")
        return f"Écho {hote}:{port}"

    def reposer(self, duree):
        print(f"Repos pour {duree} secondes")
        time.sleep(duree)
        print('Repos terminé.')

class GestionnaireAlarme(object):
    def declencher(self):
        return 'Alarme sonnée'

NOM_SERVICE_ECHO = "echo_service"
NOM_SERVICE_ALARME = "alarme_service"

def demarrer_serveur_unique():
    module_echo = thriftpy.load("echo.thrift", module_name="echo_thrift")
    serveur = make_server(module_echo.ServiceEcho, GestionnaireEcho(), '127.0.0.1', 7000)
    serveur.serve()

def demarrer_serveur_multiplexe():
    module_alarme = thriftpy.load("alarme.thrift", module_name="alarme_thrift")
    module_echo = thriftpy.load("echo.thrift", module_name="echo_thrift")
    processeur_alarme = TProcessor(module_alarme.ServiceAlarme, GestionnaireAlarme())
    processeur_echo = TProcessor(module_echo.ServiceEcho, GestionnaireEcho())
    processeur_multiplexe = TMultiplexedProcessor()
    processeur_multiplexe.register_processor(NOM_SERVICE_ALARME, processeur_alarme)
    processeur_multiplexe.register_processor(NOM_SERVICE_ECHO, processeur_echo)
    serveur = TThreadedServer(processeur_multiplexe, TServerSocket('127.0.0.1', 7000),
                              iprot_factory=TBinaryProtocolFactory(),
                              itrans_factory=TBufferedTransportFactory())
    serveur.serve()

def demarrer_serveur_avec_args():
    module_echo = thriftpy.load("echo.thrift", module_name="echo_thrift")
    serveur = make_server(module_echo.ServiceEchoAvecArgs, GestionnaireEcho(), '127.0.0.1', 7000)
    serveur.serve()

def demarrer_serveur_asynchrone():
    module_echo = thriftpy.load("echo.thrift", module_name="echo_thrift")
    serveur = make_server(module_echo.ServiceRepos, GestionnaireEcho(), '127.0.0.1', 7000)
    serveur.serve()

demarrer_serveur_asynchrone()

Client Thrift en Python

Exemple de code client correspondant :

import thriftpy
from thriftpy.rpc import make_client, client_context
from thriftpy.protocol import TBinaryProtocolFactory, TMultiplexedProtocolFactory

NOM_SERVICE_ECHO = "echo_service"
NOM_SERVICE_ALARME = "alarme_service"

def appel_simple():
    module_echo = thriftpy.load("echo.thrift", module_name="echo_thrift")
    client = make_client(module_echo.ServiceEcho, '127.0.0.1', 7000)
    print(client.appeler())

def appel_multiplexe():
    module_alarme = thriftpy.load("alarme.thrift", module_name="alarme_thrift")
    module_echo = thriftpy.load("echo.thrift", module_name="echo_thrift")
    fabrique_binaire = TBinaryProtocolFactory()
    fabrique_alarme = TMultiplexedProtocolFactory(fabrique_binaire, NOM_SERVICE_ALARME)
    with client_context(module_alarme.ServiceAlarme, '127.0.0.1', 7000,
                        proto_factory=fabrique_alarme) as c:
        print(c.declencher())

    fabrique_echo = TMultiplexedProtocolFactory(fabrique_binaire, NOM_SERVICE_ECHO)
    with client_context(module_echo.ServiceEcho, '127.0.0.1', 7000,
                        proto_factory=fabrique_echo) as c:
        print(c.appeler())

def appel_avec_arguments():
    module_echo = thriftpy.load("echo.thrift", module_name="echo_thrift")
    client = make_client(module_echo.ServiceEchoAvecArgs, '127.0.0.1', 7000)
    print(client.appeler("127.0.0.1", 7000))

def appel_asynchrone():
    module_echo = thriftpy.load("echo.thrift", module_name="echo_thrift")
    client = make_client(module_echo.ServiceRepos, '127.0.0.1', 7000)
    client.reposer(2)

appel_asynchrone()

Étiquettes: Thrift Python RPC SOA middleware

Publié le 14 juin à 19h16