コード例 #1
0
def make_server(service,
                handler,
                host="localhost",
                port=9090,
                unix_socket=None,
                proto_factory=TBinaryProtocolFactory(),
                trans_factory=TBufferedTransportFactory()):
    processor = TProcessor(service, handler)
    if unix_socket:
        server_socket = TServerSocket(unix_socket=unix_socket)
    elif host and port:
        server_socket = TServerSocket(host=host, port=port)
    else:
        raise ValueError("Either host/port or unix_socket must be provided.")

    server = TThreadedServer(processor,
                             server_socket,
                             iprot_factory=proto_factory,
                             itrans_factory=trans_factory)
    return server
コード例 #2
0
ファイル: rpc.py プロジェクト: acv-auctions/manifold
def make_server(host="localhost",
                port=9090,
                unix_socket=None,
                proto_factory=TBinaryProtocolFactory(),
                trans_factory=TBufferedTransportFactory(),
                client_timeout=3000,
                certfile=None):
    """Creates a Thrift RPC server and serves it with configuration
    """
    _init_django()

    processor = get_rpc_application()

    if unix_socket:
        server_socket = TServerSocket(unix_socket=unix_socket)
        if certfile:
            logging.error("SSL only works with host:port, not unix_socket.")
    elif host and port:
        if certfile:
            server_socket = TSSLServerSocket(host=host,
                                             port=port,
                                             client_timeout=client_timeout,
                                             certfile=certfile)
        else:
            server_socket = TServerSocket(host=host,
                                          port=port,
                                          client_timeout=client_timeout)
    else:
        raise ValueError("Either host/port or unix_socket must be provided.")

    server = TThreadedServer(processor,
                             server_socket,
                             iprot_factory=proto_factory,
                             itrans_factory=trans_factory)

    try:
        return server
    except KeyboardInterrupt:
        print()
        print("Stopping Server from Keyboard Interruption")
        exit()
コード例 #3
0
def create_multiplexed_server(services,
                              socket_config,
                              server_cls=TThreadedServer):
    """
    创建多路复用的Thrift Server
    :param services: 多路复用服务定义,如:[(service1, handler1, service_name1),
    (service2, handler2, service_name2),...]
    :param socket_config: Server的socket参数
    :param server_cls: 启动的服务器类型
    :return: Server对象
    """
    processor = TMultiplexedProcessor()
    for service, handler, service_name in services:
        processor.register_processor(service_name,
                                     TProcessor(service, handler))

    return server_cls(
        processor,  # processor
        TServerSocket(**socket_config),  # transport
        TBufferedTransportFactory(),  # transportFactory
        TBinaryProtocolFactory())  # protocolFactory
コード例 #4
0
def make_client(service,
                host="localhost",
                port=9090,
                unix_socket=None,
                proto_factory=TBinaryProtocolFactory(),
                trans_factory=TBufferedTransportFactory(),
                timeout=None):
    if unix_socket:
        socket = TSocket(unix_socket=unix_socket)
    elif host and port:
        socket = TSocket(host, port)
    else:
        raise ValueError("Either host/port or unix_socket must be provided.")

    if timeout:
        socket.set_timeout(timeout)

    transport = trans_factory.get_transport(socket)
    protocol = proto_factory.get_protocol(transport)
    transport.open()
    return TClient(service, protocol)
コード例 #5
0
ファイル: multiplexed_client.py プロジェクト: yyf1992/note
def main():
    binary_factory = TBinaryProtocolFactory()
    dd_factory = TMultiplexedProtocolFactory(binary_factory, DD_SERVICE_NAME)
    with client_context(dd_thrift.DingService,
                        '127.0.0.1',
                        9090,
                        proto_factory=dd_factory,
                        socket_timeout=100000) as c:
        # ring that doorbell
        dong = c.ding()
        print(dong)

    pp_factory = TMultiplexedProtocolFactory(binary_factory, PP_SERVICE_NAME)
    with client_context(pp_thrift.PingService,
                        '127.0.0.1',
                        9090,
                        proto_factory=pp_factory,
                        socket_timeout=100000) as c:
        # play table tennis like a champ
        pong = c.ping()
        print(pong)
コード例 #6
0
ファイル: rpc.py プロジェクト: Raoul1996/robot_view_be
def make_server(host="0.0.0.0",
                port=9090,
                unix_socket=None,
                proto_factory=TBinaryProtocolFactory(),
                trans_factory=TBufferedTransportFactory(),
                client_timeout=3000,
                certfile=None):
    """Creates a Thrift RPC server and serves it with configuration"""
    processor = create_processor()

    if unix_socket:
        server_socket = TServerSocket(unix_socket=unix_socket)
        if certfile:
            warnings.warn("SSL only works with host:port, not unix_socket.")
    elif host and port:
        if certfile:
            server_socket = TSSLServerSocket(host=host,
                                             port=port,
                                             client_timeout=client_timeout,
                                             certfile=certfile)
        else:
            server_socket = TServerSocket(host=host,
                                          port=port,
                                          client_timeout=client_timeout)
    else:
        raise ValueError("Either host/port or unix_socket must be provided.")

    server = TThreadedServer(processor,
                             server_socket,
                             iprot_factory=proto_factory,
                             itrans_factory=trans_factory)

    print('Starting Thrift RPC server running @ %s:%s' % (host, port))

    try:
        server.serve()
    except KeyboardInterrupt:
        print()
        print("Stopping Server from Keyboard Interruption")
        exit()
コード例 #7
0
ファイル: test_multiplexed.py プロジェクト: tubular/thriftpy
def server(request):
    p1 = TProcessor(mux.ThingOneService, DispatcherOne())
    p2 = TProcessor(mux.ThingTwoService, DispatcherTwo())

    mux_proc = TMultiplexedProcessor()
    mux_proc.register_processor("ThingOneService", p1)
    mux_proc.register_processor("ThingTwoService", p2)

    _server = TThreadedServer(mux_proc, TServerSocket(unix_socket=sock_path),
                              iprot_factory=TBinaryProtocolFactory(),
                              itrans_factory=TBufferedTransportFactory())
    ps = multiprocessing.Process(target=_server.serve)
    ps.start()
    time.sleep(0.1)

    def fin():
        if ps.is_alive():
            ps.terminate()
        try:
            os.remove(sock_path)
        except IOError:
            pass
    request.addfinalizer(fin)
コード例 #8
0
def get_client(thrift_file,
               thrift_name,
               url,
               port,
               service_name,
               is_multiplexer=False,
               timeout=20 * 1000):
    '''
    获取RPC client 链接
    :param thrift_file: thriftpy服务文件路径
    :param thrift_name:thritpy定义的类名称   # service 【CarsServices】
    :param url:rpc服务ip或域名
    :param port:服务端口
    :param service_name: 服务名称
    :param is_multiplexer:是否是多服务
    :return:
    '''
    try:
        my_service = getattr(
            thriftpy.load(thrift_file,
                          module_name=thrift_file.split('/')[-1].replace(
                              '.', '_')), thrift_name)
        if is_multiplexer:
            binary_factory = TBinaryProtocolFactory()
            dd_factory = TMultiplexedProtocolFactory(binary_factory,
                                                     service_name)
            client = make_client(my_service,
                                 url,
                                 port,
                                 proto_factory=dd_factory,
                                 timeout=timeout)
        else:
            client = make_client(my_service, url, port, timeout=timeout)
        return client
    except Exception as ex:
        logger.error()
        return None
コード例 #9
0
ファイル: util.py プロジェクト: motazreda/streamparse
def get_nimbus_client(env_config=None, host=None, port=None, timeout=7000):
    """Get a Thrift RPC client for Nimbus given project's config file.

    :param env_config: The project's parsed config.
    :type env_config: `dict`
    :param host: The host to use for Nimbus.  If specified, `env_config` will
                 not be consulted.
    :type host: `str`
    :param port: The port to use for Nimbus.  If specified, `env_config` will
                 not be consulted.
    :type port: `int`
    :param timeout: The time to wait (in milliseconds) for a response from
                    Nimbus.
    :param timeout: `int`

    :returns: a ThriftPy RPC client to use to communicate with Nimbus
    """
    if host is None:
        host, port = get_nimbus_host_port(env_config)
    nimbus_client = make_client(storm_thrift.Nimbus, host=host, port=port,
                                proto_factory=TBinaryProtocolFactory(),
                                trans_factory=TFramedTransportFactory(),
                                timeout=timeout)
    return nimbus_client
コード例 #10
0
 def get_protoco_factory(self):
     from thriftpy.protocol import TBinaryProtocolFactory
     return TBinaryProtocolFactory().get_protocol
コード例 #11
0
ファイル: benchmark.py プロジェクト: henrique/thriftpy
def decode(n, proto_factory=TBinaryProtocolFactory()):
    ab = make_addressbook()
    encoded = serialize(ab)
    for i in range(n):
        deserialize(ab, encoded, proto_factory)
コード例 #12
0
ファイル: benchmark.py プロジェクト: henrique/thriftpy
def encode(n, proto_factory=TBinaryProtocolFactory()):
    for i in range(n):
        ab = make_addressbook()
        serialize(ab, proto_factory)
コード例 #13
0
import os
import logging

import thriftpy
from thriftpy.rpc import make_server
from thriftpy.protocol import TBinaryProtocolFactory
from thriftpy.transport import TBufferedTransportFactory

# 使用 thriftpy(https://github.com/eleme/thriftpy)的例子

HERE = os.path.abspath(os.path.dirname(__file__))
logging.basicConfig()  # 这一步很重要,可以收到Thrift发出来的异常日志

calc_thrift = thriftpy.load(os.path.join(HERE, 'calc.thrift'),
                            module_name='calc_thrift')


class Dispatcher(object):
    def add(self, a, b):
        return a + b


server = make_server(calc_thrift.CalcService,
                     Dispatcher(),
                     '127.0.0.1',
                     8300,
                     proto_factory=TBinaryProtocolFactory(),
                     trans_factory=TBufferedTransportFactory())
print 'serving...'
server.serve()
コード例 #14
0
def make_client(service, host, port, proto_factory=TBinaryProtocolFactory()):
    transport = TBufferedTransport(TSocket(host, port))
    protocol = proto_factory.get_protocol(transport)
    transport.open()
    return TClient(service, protocol)