def test_duplicate_loads(): # multiple loads with same module_name returns the same module ab_1 = thriftpy.load("addressbook.thrift", module_name="addressbook_thrift") ab_2 = thriftpy.load("./addressbook.thrift", module_name="addressbook_thrift") assert ab_1 == ab_2
def test_isinstancecheck(): ab = thriftpy.load("addressbook.thrift") ab2 = thriftpy.load("addressbook.thrift") assert isinstance(ab.Person(), ab2.Person) assert isinstance(ab.Person(name="hello"), ab2.Person) assert isinstance(ab.PersonNotExistsError(), ab2.PersonNotExistsError)
def init_conn(ip, port): if not os.path.exists("weightsync.thrift"): f = _get_thrift_file() weightsync_thrift = thriftpy.load(f.name, module_name="weightsync_thrift") f.close() else: weightsync_thrift = thriftpy.load("weightsync.thrift", module_name="weightsync_thrift") client = make_client(weightsync_thrift.WeightSync, ip, port) return client
def init_sender(ip, port): if not os.path.exists("weightsync.thrift"): f = _get_thrift_file() weightsync_thrift = thriftpy.load(f.name, module_name="weightsync_thrift") f.close() else: weightsync_thrift = thriftpy.load("weightsync.thrift", module_name="weightsync_thrift") sender = make_client(weightsync_thrift.WeightForward, ip, port) return sender
def init_receiver(ip, port, requestHandler): if not os.path.exists("weightsync.thrift"): f = _get_thrift_file() weightsync_thrift = thriftpy.load(f.name, module_name="weightsync_thrift") f.close() else: weightsync_thrift = thriftpy.load("weightsync.thrift", module_name="weightsync_thrift") receiver = make_server(weightsync_thrift.WeightForward, requestHandler, ip, port) receiver.trans.client_timeout = None return receiver
def init_server(ip, port, requestHandler): if not os.path.exists("weightsync.thrift"): f = _get_thrift_file() weightsync_thrift = thriftpy.load(f.name, module_name="weightsync_thrift") f.close() else: weightsync_thrift = thriftpy.load("weightsync.thrift", module_name="weightsync_thrift") server = make_server(weightsync_thrift.WeightSync, requestHandler, ip, port) return server
def test_load(): ab_1 = thriftpy.load("addressbook.thrift") ab_2 = thriftpy.load("addressbook.thrift", module_name="addressbook_thrift") assert ab_1.__name__ == "addressbook" assert ab_2.__name__ == "addressbook_thrift" # load without module_name can't do pickle with pytest.raises(pickle.PicklingError): pickle.dumps(ab_1.Person(name='Bob')) # load with module_name set and it can be pickled person = ab_2.Person(name='Bob') assert person == pickle.loads(pickle.dumps(person))
def main(): print("Fetching status of local gdcv server...") gdcv_thrift = thriftpy.load('gdcv/if/gdcv.thrift', module_name='gdcv_thrift') client = make_client(gdcv_thrift.FrcRealtimeScoringService, '127.0.0.1', 6000) print("{} v{} is {}, since {}".format(client.getName(), client.getVersion(), client.getStatus(), client.aliveSince())) metadataKey = "testKey" print("Testing metadata server... {}={}".format( metadataKey, client.getMetadataValue(metadataKey))) print("Testing Pub/Sub...") testMessage = "Hello World!" result = client.sendPubSubMessage(testMessage) print("Result from pubsub send: {}".format(result)) result = client.getPubSubMessage() print("Got message {} through local Pub/Sub".format(result)) print("Testing db...") testMessage = "Hello World!" client.clearAllTestMessages() result = client.insertTestRow(testMessage) print("All rows: {}".format(client.getAllTestMessages())) print("Testing frc-livescore...") print("Test image: {}".format(client.processTestImage()))
def ps_job(ps_id, cluster_spec, dim): signal.signal(signal.SIGINT, soft_exit) ping_thrift = thriftpy.load("ping.thrift", module_name="ping_thrift") handler = Dispatcher(dim) server = make_server(ping_thrift.PingService, handler, '127.0.0.1', 6000) print "start Server(%s:%d)" % ('127.0.0.1', 6000) server.serve()
def setUp(self): test_thrift = thriftpy.load("test.thrift", module_name="test_thrift") self._output_dict = dict() self._thrift_struct = test_thrift.meta() self._thrift_struct.name = "abc" self._thrift_struct.num = 128 self._thrift_struct.desc = "for_lyj_test" self._thrift_struct.error_type = 1 self._thrift_struct.simple_list = [1, 2, 3, 4] tmp_list = [] for i in xrange(1,5): tmp = test_thrift.list_test() tmp.l_name = "l_name%d" % i tmp_list.append(tmp) self._thrift_struct.in_list = tmp_list tmp = test_thrift.in_meta() tmp.more = "more" self._thrift_struct.add_more = tmp tmp_map = dict() map_value = test_thrift.test_map_value() map_value.value = "abc" tmp_map["a"] = map_value map_value = test_thrift.test_map_value() map_value.value = "abc1" tmp_map["b"] = map_value self._thrift_struct.test_struct_map = tmp_map tmp_map = {"a": 1, "b": 2} self._thrift_struct.test_simple_map = tmp_map thrift_json_convertor(self._thrift_struct, self._output_dict)
def serve(thrift_filename, service_name, contract_filename, host="127.0.0.1", port=6000, protocol=Protocol.binary, transport=Transport.framed): # Loads both the AST and the thriftpy dynamically generated data ast = ptcdt.thrift_parser.MappedAST.from_file(thrift_filename) thriftpy_module = thriftpy.load(thrift_filename, module_name=service_name + "_thrift") contract = ptcdt.contracts.parse_contract(contract_filename) # Builds the delegate class which will have all the methods to handle the requests # The delegate will have the methods dynamically added into them, which actually point # to methods in a new instance of FunctionDelegate objects Delegate = build_delegate( _ServiceExecutionContext(ast, thriftpy_module, contract, service_name)) # Builds the server and starts serving requests server = make_server(service=getattr(thriftpy_module, service_name), handler=Delegate(), host=host, port=port, proto_factory=_build_protocol_factory(protocol), trans_factory=_build_transport_factory(transport)) server.serve()
def cn_job(cn_id, cluster_spec, times): signal.signal(signal.SIGINT, soft_exit) ping_thrift = thriftpy.load("ping.thrift", module_name="ping_thrift") conn = make_client(ping_thrift.PingService, '127.0.0.1', 6000) for i in range(times): compute_pi(100000) conn.ping(cn_id)
def client(thrift, host, port, service): calc_thrift = thriftpy.load(str(thrift)) with client_context(getattr(calc_thrift, service), host, port, proto_factory=TCyBinaryProtocolFactory(), trans_factory=TCyBufferedTransportFactory()) as client: IPython.embed(header="Call client.api(*args)")
def main(): ''' ''' example_thrift = thriftpy.load("rpc_example.thrift", module_name="pp_thrift") with client_context(example_thrift.example, '127.0.0.1', 6000) as c: print c.ping()
def todo_thrift(): return thriftpy.load( os.path.join( os.path.dirname(os.path.realpath(__file__)), "..", "example-thrifts", "todo.thrift", ))
def __init__(self): if thriftpy._compat.CYTHON: self.NAME += '+cython' self.module = thriftpy.load( 'specs/test.thrift', include_dirs=['specs'], )
def request_thrift(thrift_name, service, method, url, port, *args, **kwargs): ''' 通用的 simpled rpc client ''' module_name = thrift_name.replace('.', '_') thrift_module = thriftpy.load(thrift_name, module_name=module_name) thrift_service = getattr(thrift_module, service) with client_context(thrift_service, url, port) as rt: res = getattr(rt, method)(*args, **kwargs) print(res)
def __init__(self, api_key, host, port): self.api_key = api_key trade_thrift = thriftpy.load("traderpc.thrift", module_name="trade_thrift") self.client = make_client(trade_thrift.TradeRpcService, host, port, timeout=5000)
def test_hashable(): ab = thriftpy.load("addressbook.thrift") # exception is hashable hash(ab.PersonNotExistsError("test error")) # container struct is not hashable with pytest.raises(TypeError): hash(ab.Person(name="Tom"))
def __init__(self, event_handler=EventChannelInterfaceHandler()): """Initialize the ThriftClient object.""" self._event_channel = None self._event_channel_handler = event_handler _interface_file = os.path.join( os.path.dirname(os.path.abspath(__file__)), "interface.thrift") self.thrift_interface = thriftpy.load(_interface_file, module_name="zlaser_thrift")
def connect(self, ip_addr, port, thrift_file_path): self.ui_thrift = thriftpy.load(thrift_file_path) self.THRIFT_SMS_FMAN_DATA = self.ui_thrift.THRIFT_SMS_FMAN_DATA self.THRIFT_MAP_IMAGE = self.ui_thrift.THRIFT_MAP_IMAGE self.THRIFT_SENSOR_DATA = self.ui_thrift.THRIFT_SENSOR_DATA self.THRIFT_IPLIMAGE = self.ui_thrift.THRIFT_IPLIMAGE self.THRIFT_QIMAGE = self.ui_thrift.THRIFT_QIMAGE self.client = make_client(self.ui_thrift.Control, ip_addr, port)
def _load_thrifts(thrift_directory): thrifts = {} thrift_paths = {} search_path = os.path.join(thrift_directory, "**/*thrift") for thrift_path in glob.iglob(search_path, recursive=True): thrift_filename = os.path.basename(thrift_path) thrifts[thrift_filename] = thriftpy.load(thrift_path) thrift_paths[thrift_filename] = thrift_path return thrifts, thrift_paths
def _start_thrift_service(): import thriftpy tutor_thrift = thriftpy.load('thriftfiles/tutor.thrift', 'tutor_thrift', include_dirs=['thriftfiles']) tutor_urge_thrift = thriftpy.load('thriftfiles/tutor_urge.thrift', 'tutor_urge_thrift', include_dirs=['thriftfiles']) from etutorservice.services.season import SeasonService from etutorservice.services.period import PeriodService from etutorservice.services.class_template import ClassTemplateService from etutorservice.services.coach import CoachService from etutorservice.services.student import StudentService from etutorservice.services.quota import QuotaService from etutorservice.services.tutor_client import TutorClientService from etutorservice.services.class_admin import ClassAdminService from etutorservice.services.class_service import ClassService from etutorservice.services.coach_client import CoachClientService from etutorservice.services.monitor import MonitorService from etutorservice.services.urge_class import UrgeClassService from etutorservice.services.sms_code import SmsCodeService from etutorservice.services.experience_center import ExperienceService from etutorservice.services.payment import PaymentService thrift_config = config.data['thrift_service']['tutor'] server = create_multiplexed_server( [ (tutor_thrift.SeasonService, SeasonService(), 'season_service'), (tutor_thrift.PeriodService, PeriodService(), 'period_service'), (tutor_thrift.ClassTemplateService, ClassTemplateService(), 'class_template_service'), (tutor_thrift.CoachService, CoachService(), 'coach_service'), (tutor_thrift.StudentService, StudentService(), 'student_service'), (tutor_thrift.ClassAdminService, ClassAdminService(), 'class_admin_service'), (tutor_thrift.ClassService, ClassService(), 'class_service'), (tutor_thrift.QuotaService, QuotaService(), 'quota_service'), (tutor_thrift.TutorClientService, TutorClientService(), 'tutor_client_service'), (tutor_thrift.CoachClientService, CoachClientService(), 'coach_client_service'), (tutor_thrift.MonitorService, MonitorService(), 'monitor_service'), (tutor_thrift.SmsCodeService, SmsCodeService(), 'sms_code_service'), (tutor_thrift.PaymentService, PaymentService(), 'payment_service'), (tutor_urge_thrift.UrgeClassService, UrgeClassService(), 'urge_class_service'), (tutor_thrift.ExperienceService, ExperienceService(), 'experience_service'), ], thrift_config) server.serve()
def request_thrift(thrift_name,service, method, url, port, *args, **kwargs): ''' 通用的 multiplexed ''' module_name = thrift_name.replace('.', '_') thrift_module = thriftpy.load(thrift_name, module_name=module_name) thrift_service = getattr(thrift_module, service) binary_factory = TBinaryProtocolFactory() thrift_factory = TMultiplexedProtocolFactory(binary_factory, module_name) with client_context(thrift_service, url, port, proto_factory=thrift_factory) as rt: res = getattr(rt, method)(*args, **kwargs) print(res)
def __init__(self): super(ThriftLocust, self).__init__() socket = TSocket('127.0.0.1', 9999) proto_factory = TBinaryProtocolFactory() trans_factory = TBufferedTransportFactory() transport = trans_factory.get_transport(socket) protocol = proto_factory.get_protocol(transport) transport.open() hash_thrift = thriftpy.load("hash.thrift", module_name="hash_thrift") self.client = ThriftClient(hash_thrift.HashService, protocol)
def put(self, *args, **kwargs): self.project = make_client( thriftpy.load(pjoin(protocols, "project.thrift"), module_name="project_thrift").ProjectHandle, port=6000) # program = kwargs.get("program") body = json.loads(self.request.body) self.write(self.project.update_project(json.dumps(body))) self.project.close()
def pingpong_thrift_service(request, pingpong_service_key): import thriftpy thrift_service = thriftpy.load( os.path.join( os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "examples/pingpong_app/pingpong.thrift"), "pingpong_thrift") service = thrift_service.PingService service.AboutToShutDownException = \ thrift_service.AboutToShutDownException return service
def load_thrift(grammar_file=None): """ Loads and compiles an Apache Thrift file into a module. :param grammar_file: The file path to the Apache Thrift file. :return: The compiled module. """ import os return thriftpy.load(grammar_file, module_name=os.path.splitext(os.path.basename(grammar_file))[0] + '_thrift')
def main(): host = '127.0.0.1' if len(sys.argv) < 2 else sys.argv[1] port = 9090 if len(sys.argv) < 3 else int(sys.argv[2]) uniqueid_thrift = thriftpy.load('../protocol/uniqueid.thrift', module_name='uniqueid_thrift') client = make_client(uniqueid_thrift.Uniqueid, host, port) request = uniqueid_thrift.UniqueidRequest() request.logid = random.randint(-2147483648, 2147483647) request.serial = random.randint(0, 9) request.length = random.randint(1, 10) response = client.uniqueid(request) print(response)
def post(self, *args, **kwargs): self.project = make_client( thriftpy.load(pjoin(protocols, "project.thrift"), module_name="project_thrift").ProjectHandle, port=6000) self.write( self.project.do_actions( json.dumps( dict(programs=self.get_argument("programs", []), actions=self.get_argument("actions", []))))) self.project.close()
def delete(self, *args, **kwargs): self.project = make_client( thriftpy.load(pjoin(protocols, "project.thrift"), module_name="project_thrift").ProjectHandle, port=6000) if __debug__: print self.request.body body = json.loads(self.request.body) self.write( self.project.remove_projects(json.dumps(body.get("programs", [])))) self.project.close()
class SpotifakeServerLibraryHandler(LibraryService.Iface): connection: SqlServerLibraryManagement = SqlServerLibraryManagement() spotifakeManagement_thrift = thriftpy.load( '../Thrift/SpotifakeManagement.thrift', module_name='spotifakeManagement_thrift') spotifakeServices_thrift = thriftpy.load( '../Thrift/SpotifakeServices.thrift', module_name='spotifakeServices_thrift') Library = spotifakeManagement_thrift.Library def __init__(self): pass def getLibraryByIdConsumer(self, idConsumer): idLibrary = SqlServerLibraryManagement.GetLibraryByIdConsumer( self, idConsumer) return idLibrary def AddLibrary(self, idConsumer): idLibrary = SqlServerLibraryManagement.AddLibrary(self, idConsumer) return True
def test_delegate_simple_param_return(): filename = utils.test_resource_path("thrifts/echo.thrift") ast = MappedAST.from_file(filename) thriftpy_module = thriftpy.load(filename, module_name="echo_thrift") contract = Contract(Provider("provider"), Consumer("consumer"), [ Interaction("provider state", "description", "Echo", Request("echo", ["hello"]), Response("hello")) ]) Delegate = build_delegate( ptcdt.server._ServiceExecutionContext(ast, thriftpy_module, contract, "Echo")) assert Delegate().echo("hello") == "hello"
def test_parse_spec(): ab = thriftpy.load("addressbook.thrift") cases = [ ((TType.I32, None), "I32"), ((TType.STRUCT, ab.PhoneNumber), "PhoneNumber"), ((TType.LIST, TType.I32), "LIST<I32>"), ((TType.LIST, (TType.STRUCT, ab.PhoneNumber)), "LIST<PhoneNumber>"), ((TType.MAP, (TType.STRING, ( TType.LIST, (TType.MAP, (TType.STRING, TType.STRING))))), "MAP<STRING, LIST<MAP<STRING, STRING>>>") ] for spec, res in cases: assert parse_spec(*spec) == res
def test_import_hook(): ab_1 = thriftpy.load("addressbook.thrift") print("Load file succeed.") assert ab_1.DEFAULT_LIST_SIZE == 10 try: import addressbook_thrift as ab # noqa except ImportError: print("Import hook not installed.") thriftpy.install_import_hook() import addressbook_thrift as ab_2 print("Magic import succeed.") assert ab_2.DEFAULT_LIST_SIZE == 10
def pingpong_thrift_service(request, pingpong_service_key): import thriftpy thrift_service = thriftpy.load( os.path.join( os.path.dirname( os.path.dirname( os.path.dirname(__file__) ) ), "examples/pingpong_app/pingpong.thrift"), "pingpong_thrift") service = thrift_service.PingService service.AboutToShutDownException = \ thrift_service.AboutToShutDownException return service
def setUp(self): self._input_json = '{ \ "name": "abc", \ "num": 128, \ "desc": "for_lyj_test", \ "simple_list": [1,2,3,4], \ "add_more": {"more": "more"}, \ "in_list": [{"l_name": "l_name1"}, {"l_name": "l_name2"}, {"l_name": "l_name3"}], \ "error_type": "SUCCESS", \ "test_struct_map" : {\ "a": {"value": "abc"}, \ "b": {"value": "abc1"} \ }, \ "test_simple_map" : {"a": 1, "b": 2} \ }' test_thrift = thriftpy.load("test.thrift", module_name="test_thrift") self._json_struct = json.loads(self._input_json) self._thrift_struct = test_thrift.meta() json_thrift_convertor(self._json_struct, self._thrift_struct)
def _load_thrift(): """Load the Scribe Thrift specification. The Thrift files we have are borrowed from the facebook-scribe repository here: https://github.com/tomprimozic/scribe-python As Scribe is now abandoned (per https://github.com/facebookarchive/scribe), it's highly unlikely that the scribe format will ever change. """ # In the event that pkg_resources has to extract the thrift files from e.g. # a zip file to some temporary place, this asks it to clean them up atexit.register(pkg_resources.cleanup_resources) # Call this and discard the return value, just to ensure that this file is # available on the filesystem; it's included by scribe.thrift pkg_resources.resource_filename('clog', 'fb303.thrift') path = pkg_resources.resource_filename('clog', 'scribe.thrift') include_dir, fn = os.path.split(path) return thriftpy.load( fn, module_name='scribe_thrift', include_dirs=[include_dir])
from impala._thrift_gen.beeswax import BeeswaxService from impala._thrift_gen.ImpalaService import ImpalaService from impala._thrift_gen.Status.ttypes import TStatus, TStatusCode from impala._thrift_gen.ExecStats.ttypes import TExecStats from impala._thrift_gen.beeswax.BeeswaxService import QueryState ThriftClient = ImpalaService.Client if six.PY3: # import thriftpy code from thriftpy import load from thriftpy.thrift import TClient, TApplicationException # dynamically load the beeswax modules from impala._thrift_api import thrift_dir ExecStats = load(os.path.join(thrift_dir, 'ExecStats.thrift'), include_dirs=[thrift_dir]) Status = load(os.path.join(thrift_dir, 'Status.thrift'), include_dirs=[thrift_dir]) ImpalaService = load(os.path.join(thrift_dir, 'ImpalaService.thrift'), include_dirs=[thrift_dir]) beeswax = load(os.path.join(thrift_dir, 'beeswax.thrift'), include_dirs=[thrift_dir]) sys.modules[ExecStats.__name__] = ExecStats sys.modules[Status.__name__] = Status sys.modules[ImpalaService.__name__] = ImpalaService sys.modules[beeswax.__name__] = beeswax # import the beeswax objects from ExecStats import TExecStats from Status import TStatus, TStatusCode from ImpalaService import ImpalaService
#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'Jack River' import warnings import json import os import thriftpy from thrift_connector import ClientPool, ThriftPyCyClient _thrift_file = thriftpy.load( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'hermes.thrift') ) HermesSystemException = _thrift_file.HermesSystemException HermesUserException = _thrift_file.HermesUserException HermesUnknownException = _thrift_file.HermesUnknownException HermesErrorCode = _thrift_file.HermesErrorCode def deprecated(func): """This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.""" def new_func(*args, **kwargs): warnings.warn("Call to deprecated function {}.".format(func.__name__), category=DeprecationWarning) return func(*args, **kwargs) new_func.__name__ = func.__name__
if six.PY3: # import thriftpy code from thriftpy import load from thriftpy.thrift import TClient # TODO: reenable cython # from thriftpy.protocol import TBinaryProtocol from thriftpy.protocol.binary import TBinaryProtocol # noqa from thriftpy.transport import TSocket, TTransportException # noqa # TODO: reenable cython # from thriftpy.transport import TBufferedTransport from thriftpy.transport.buffered import TBufferedTransport # noqa thrift_dir = os.path.join(os.path.dirname(__file__), 'thrift') # dynamically load the HS2 modules ExecStats = load(os.path.join(thrift_dir, 'ExecStats.thrift'), include_dirs=[thrift_dir]) TCLIService = load(os.path.join(thrift_dir, 'TCLIService.thrift'), include_dirs=[thrift_dir]) ImpalaService = load(os.path.join(thrift_dir, 'ImpalaService.thrift'), include_dirs=[thrift_dir]) sys.modules[ExecStats.__name__] = ExecStats sys.modules[TCLIService.__name__] = TCLIService sys.modules[ImpalaService.__name__] = ImpalaService # import the HS2 objects from TCLIService import ( # noqa TOpenSessionReq, TFetchResultsReq, TCloseSessionReq, TExecuteStatementReq, TGetInfoReq, TGetInfoType, TTypeId, TFetchOrientation, TGetResultSetMetadataReq, TStatusCode, TGetColumnsReq, TGetSchemasReq, TGetTablesReq, TGetFunctionsReq, TGetOperationStatusReq, TOperationState, TCancelOperationReq,
# -*- coding: utf-8 -*- import thriftpy from thriftpy.protocol import TBinaryProtocolFactory from thriftpy.server import TThreadedServer from thriftpy.thrift import TProcessor, TMultiplexedProcessor from thriftpy.transport import TBufferedTransportFactory, TServerSocket dd_thrift = thriftpy.load("dingdong.thrift", module_name="dd_thrift") pp_thrift = thriftpy.load("pingpong.thrift", module_name="pp_thrift") DD_SERVICE_NAME = "dd_thrift" PP_SERVICE_NAME = "pp_thrift" class DingDispatcher(object): def ding(self): print("ding dong!") return 'dong' class PingDispatcher(object): def ping(self): print("ping pong!") return 'pong' def main(): dd_proc = TProcessor(dd_thrift.DingService, DingDispatcher()) pp_proc = TProcessor(pp_thrift.PingService, PingDispatcher())
import thriftpy import os.path import thriftpy thrift_file = os.path.join(os.path.dirname(__file__), "lang.thrift") lang_thrift = thriftpy.load(thrift_file, module_name="lang_thrift")
def __init__(self, address, port=9196): thrift = load(resource_filename(__name__, "static/yaoguang.thrift"), module_name="yaoguang_thrift") self._client = make_client(thrift.ThriftInterface, address, port)
import thriftpy pingpong_thrift = thriftpy.load("pingpong.thrift", module_name="pingpong_thrift") from thriftpy.rpc import make_client client = make_client(pingpong_thrift.PingPong, '127.0.0.1', 6000) client.ping()
from __future__ import absolute_import import os import thriftpy cassandra_thrift = thriftpy.load(os.path.join(os.path.dirname(os.path.realpath(__file__)), "cassandra-v20.thrift"), module_name="cassandra_thrift")
from os import path from unittest import TestCase from tornado import ioloop import thriftpy from thriftpy.tornado import make_server from thriftpy.rpc import make_client from thriftpy.transport.framed import TFramedTransportFactory from thriftpy.protocol.binary import TBinaryProtocolFactory from thriftpy._compat import CYTHON logging.basicConfig(level=logging.INFO) addressbook = thriftpy.load(path.join(path.dirname(__file__), "addressbook.thrift")) class Dispatcher(object): def __init__(self, io_loop): self.io_loop = io_loop self.registry = {} def add(self, person): """ bool add(1: Person person); """ if person.name in self.registry: return False self.registry[person.name] = person return True
# -*- coding: utf-8 -*- import os import socket import struct import thriftpy import ipaddress from thriftpy.transport import TMemoryBuffer from thriftpy.protocol.binary import TBinaryProtocol thrift_filepath = os.path.join(os.path.dirname(__file__), 'zipkinCore.thrift') zipkin_core = thriftpy.load(thrift_filepath, module_name="zipkinCore_thrift") def create_annotation(timestamp, value, host): """ Create a zipkin annotation object :param timestamp: timestamp of when the annotation occured in microseconds :param value: name of the annotation :param host: zipkin endpoint object :returns: zipkin annotation object """ return zipkin_core.Annotation(timestamp=timestamp, value=value, host=host) def create_binary_annotation(key, value, annotation_type, host): """
from sys import argv from uuid import uuid4 from multiprocessing import Process, Value from thriftpy import load from thriftpy.rpc import make_client from thriftpy.transport import TFramedTransportFactory echo_thrift = load("/app/sh/echo.thrift", module_name="echo_thrift") def handleClient(num, actual): # Open independent client connection client = make_client(service=echo_thrift.Echo, host='127.0.0.1', port=9999, trans_factory=TFramedTransportFactory()) # UUID uid = str(uuid4()) for i in range(num): # Make thrift call and increment atomic count txt = uid + str(i) ret = client.echo(echo_thrift.Message(text=txt)) if (txt == ret): with actual.get_lock(): actual.value += 1 # Parse command line arguments # Number of requests each client will make num = int(argv[1]) # Number of concurrent clients to run
def test_init_func(): thriftpy.load("addressbook.thrift") assert linecache.getline('<generated PhoneNumber.__init__>', 1) != ''
import time import thriftpy play_thrift = thriftpy.load("play.thrift", module_name="play_thrift") #from thriftpy.rpc import make_server from thriftpy.tornado import make_server from thriftpy.tornado import make_client from tornado import ioloop class PlayHandler(object): def __init__(self): self._count = 0 self._st_time = time.time() def ping(self): self._count += 1 if self._count % 10000 == 0: right_now = time.time() print self._count / (right_now - self._st_time) self._st_time = right_now self._count = 0 #print "ping!" return 123
import time import pytest import thriftpy from thriftpy.protocol import ( TBinaryProtocolFactory, TMultiplexedProtocolFactory ) from thriftpy.rpc import client_context from thriftpy.server import TThreadedServer from thriftpy.thrift import TProcessor, TMultiplexedProcessor from thriftpy.transport import TBufferedTransportFactory, TServerSocket mux = thriftpy.load(os.path.join(os.path.dirname(__file__), "multiplexed.thrift")) sock_path = "./thriftpy_test.sock" class DispatcherOne(object): def doThingOne(self): return True class DispatcherTwo(object): def doThingTwo(self): return True @pytest.fixture(scope="module") def server(request):
# coding: utf-8 import os import thriftpy import json import logging from thriftpy.rpc import make_client LIMIT = 1000 logger = logging.getLogger(__name__) articlemeta_thrift = thriftpy.load( os.path.join(os.path.dirname(__file__)) + '/articlemeta.thrift') class ServerError(Exception): def __init__(self, message=None): self.message = message or 'thirftclient: ServerError' def __str__(self): return repr(self.message) class ArticleMeta(object): def __init__(self, address, port): """ Cliente thrift para o Articlemeta.
# coding: utf-8 import os import thriftpy import json import logging from thriftpy.rpc import make_client from xylose.scielodocument import Article, Journal LIMIT = 1000 logger = logging.getLogger(__name__) ratchet_thrift = thriftpy.load( os.path.join(os.path.dirname(__file__))+'/ratchet.thrift') articlemeta_thrift = thriftpy.load( os.path.join(os.path.dirname(__file__))+'/articlemeta.thrift') citedby_thrift = thriftpy.load( os.path.join(os.path.dirname(__file__))+'/citedby.thrift') accessstats_thrift = thriftpy.load( os.path.join(os.path.dirname(__file__))+'/access_stats.thrift') publication_stats_thrift = thriftpy.load( os.path.join(os.path.dirname(__file__))+'/publication_stats.thrift') class ServerError(Exception): def __init__(self, message=None): self.message = message or 'thirftclient: ServerError'
# coding=utf-8 import os import thriftpy from thriftpy.rpc import client_context from thriftpy.protocol import TBinaryProtocolFactory from thriftpy.transport import TBufferedTransportFactory HERE = os.path.abspath(os.path.dirname(__file__)) calc_thrift = thriftpy.load( os.path.join(HERE, 'calc.thrift'), module_name='calc_thrift') with client_context(calc_thrift.CalcService, '127.0.0.1', 8300, proto_factory=TBinaryProtocolFactory(), trans_factory=TBufferedTransportFactory(), timeout=None) as calc: rs = calc.add(1, 2) print 'Result is: {}'.format(rs)