Exemplo n.º 1
0
        async def g(*args, **kwargs):
            if conf.IS_FAILOVER:
                if downgrade:
                    return await f(None, *args, **kwargs)
                else:
                    return None

            res = None
            opened = False
            re_conn = TTornado.TTornadoStreamTransport(conf.CONTENT_SVC['HOST'], conf.CONTENT_SVC['PORT'])

            try:
                re_conn.open()
                opened = True
                tclient = RecommendService.Client(re_conn, TCompactProtocolFactory())
                res = await timeout(f(tclient, *args, **kwargs), TIMEOUT)
            except Exception as e:
                logger.error(f"{kwargs['log_id']} {e}")

                # 降级到Redis中取数据
                if downgrade:
                    res = await timeout(f(None, *args, **kwargs), 1)
            finally:
                if opened:
                    re_conn.close()

            return res
Exemplo n.º 2
0
def communicate():
    # create client
    transport = TTornado.TTornadoStreamTransport('localhost', 9090)
    # open the transport, bail on error
    try:
        yield transport.open()
        print('Transport is opened')
    except TTransport.TTransportException as ex:
        logging.error(ex)
        raise gen.Return()

    pfactory = TBinaryProtocol.TBinaryProtocolFactory()
    client = Calculator.Client(transport, pfactory)

    # ping
    yield client.ping()
    print("ping()")

    # add
    sum_ = yield client.add(1, 1)
    print("1 + 1 = {0}".format(sum_))

    # make a oneway call without a callback (schedule the write and continue
    # without blocking)
    client.zip()
    print("zip() without callback")

    # make a oneway call with a callback (we'll wait for the stream write to
    # complete before continuing)
    client.zip()
    print("zip() with callback")

    # calculate 1/0
    work = Work()
    work.op = Operation.DIVIDE
    work.num1 = 1
    work.num2 = 0

    try:
        quotient = yield client.calculate(1, work)
        print("Whoa? You know how to divide by zero ? -> {0}".format(quotient))
    except InvalidOperation as io:
        print("InvalidOperation: {0}".format(io))

    # calculate 15-10
    work.op = Operation.SUBTRACT
    work.num1 = 15
    work.num2 = 10

    diff = yield client.calculate(1, work)
    print("15 - 10 = {0}".format(diff))

    # getStruct
    log = yield client.getStruct(1)
    print("Check log: {0}".format(log.value))

    # close the transport
    client._transport.close()
    raise gen.Return()
Exemplo n.º 3
0
    def connect(self):
        """建立连接"""

        if isinstance(self.thrift_module, six.string_types):
            module = load_module(self.thrift_module)

        self.recorder('INFO', '{obj} connect start'.format(obj=self))
        self._transport = TTornado.TTornadoStreamTransport(
            self.host, self.port)
        yield self._connect()
        self.recorder('INFO', '{obj} connect successful'.format(obj=self))
        protocol = TCompactProtocol.TCompactProtocolFactory()
        self._client = getattr(module, 'Client')(self._transport, protocol)
        self.other = self._client
        raise Return(self)
Exemplo n.º 4
0
    def setUp(self):
        super(ThriftTestCase, self).setUp()

        self.port = get_unused_port()

        # server
        self.handler = TestHandler(self)
        self.processor = ThriftTest.Processor(self.handler)
        self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()

        self.server = TTornado.TTornadoServer(self.processor, self.pfactory, io_loop=self.io_loop)
        self.server.bind(self.port)
        self.server.start(1)

        # client
        transport = TTornado.TTornadoStreamTransport('localhost', self.port, io_loop=self.io_loop)
        pfactory = TBinaryProtocol.TBinaryProtocolFactory()
        self.io_loop.run_sync(transport.open)
        self.client = ThriftTest.Client(transport, pfactory)
Exemplo n.º 5
0
    def setUp(self):
        self.port = get_unused_port()
        self.io_loop = self.get_new_ioloop()

        # server
        self.handler = TestHandler(self)
        self.processor = ThriftTest.Processor(self.handler)
        self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()

        self.server = TTornado.TTornadoServer(self.processor, self.pfactory)
        self.server.bind(self.port)
        self.server.start(1)

        # client
        transport = TTornado.TTornadoStreamTransport('localhost', self.port)
        pfactory = TBinaryProtocol.TBinaryProtocolFactory()
        self.client = ThriftTest.Client(transport, pfactory)
        transport.open(callback=self.stop)
        self.wait(timeout=1)
Exemplo n.º 6
0
    async def g(*args, **kwargs):
        if conf.IS_FAILOVER:
            return None

        res = None
        opened = False
        di_conn = TTornado.TTornadoStreamTransport(conf.DI_CONTENT_SVC['HOST'], conf.DI_CONTENT_SVC['PORT'])

        try:
            di_conn.open()
            opened = True
            diclient = DIService.Client(di_conn, TCompactProtocolFactory())
            res = await timeout(f(diclient, *args, **kwargs), TIMEOUT)
        except Exception as e:
            logger.error(f"{kwargs['log_id']} {e}")
        finally:
            if opened:
                di_conn.close()

        return res
Exemplo n.º 7
0
def communicate():
    # create client
    transport = TTornado.TTornadoStreamTransport('localhost', 7777)
    # open the transport, bail on error
    try:
        yield transport.open()
        print('Transport is opened')
    except TTransport.TTransportException as ex:
        logging.error(ex)
        raise gen.Return()

    protocol = TCompactProtocol.TCompactProtocolFactory()
    #pfactory = TMultiplexedProtocol.TMultiplexedProtocol(protocol, 'hello')
    client = HelloService.Client(transport, protocol)

    # ping
    yield client.sayHello()
    print("ping()")

    client._transport.close()
    raise gen.Return()