def test_msgpack_numpy():
    class Srv(object):
        def echo(self, args):
            return args

    endpoint = random_ipc_endpoint()

    context = Context()
    context.register_serializer("msgpack_numpy")

    module = Srv()
    server = zerorpc.Server(module, context=context)
    server.bind(endpoint)
    gevent.spawn(server.run)

    client = zerorpc.Client(context=context)
    client.connect(endpoint)

    arg = np.arange(10)
    res = client.echo(arg)
    print(res)
    assert isinstance(res, np.ndarray)
    assert res.dtype == arg.dtype

    client.close()
    server.close()
Beispiel #2
0
def run_rpc_server(multivisor, bind, future_server):
    multivisor._log.info("0RPC: spawn server on {}...".format(os.getpid()))
    watcher = hub.get_hub().loop.async_()
    stop_event = threading.Event()
    watcher.start(lambda: spawn(multivisor._dispatch_event))
    server = None
    try:
        context = Context()
        context.register_middleware(ServerMiddleware())
        server = Server(multivisor, context=context)
        server._stop_event = stop_event
        server.bind(bind)
        future_server.put((server, watcher))
        multivisor._log.info("0RPC: server running!")
        server.run()
        multivisor._log.info("0RPC: server stopped!")
    except Exception as err:
        future_server.put(err)
    finally:
        watcher.stop()
        del server
        # prevent reusage of this loop because supervisor closes all ports
        # when a restart happens. It actually doesn't help preventing a crash
        hub.get_hub().destroy(destroy_loop=True)
        multivisor._log.info("0RPC: server thread destroyed!")
    stop_event.set()
def test_pickle_numpy():
    class Srv(object):
        def echo(self, *args):
            return args

    endpoint = random_ipc_endpoint()

    context = Context()
    context.register_serializer("pickle")

    module = Srv()
    server = zerorpc.Server(module, context=context)
    server.bind(endpoint)
    gevent.spawn(server.run)

    client = zerorpc.Client(context=context)
    client.connect(endpoint)

    args = np.zeros((10, 10), dtype=np.float32)
    res = client.echo(args)[0]
    assert isinstance(res, np.ndarray)
    assert res.dtype == np.float32

    client.close()
    server.close()
def test_pickle_cycle():
    class Srv(object):
        def echo(self, *args):
            return args

    endpoint = random_ipc_endpoint()

    context = Context()
    context.register_serializer("pickle")

    module = Srv()
    server = zerorpc.Server(module, context=context)
    server.bind(endpoint)
    gevent.spawn(server.run)

    client = zerorpc.Client(context=context)
    client.connect(endpoint)

    args = cycle((1, 2, 3))
    res = client.echo(args)[0]
    assert isinstance(res, cycle)
    # assert res == args

    client.close()
    server.close()
def test_kwargs_msgpack_numpy():
    class Srv(object):
        def echo(self, *args, **kwargs):
            return args, kwargs

    endpoint = random_ipc_endpoint()

    context = Context()
    context.register_serializer("msgpack_numpy")

    module = Srv()
    server = zerorpc.Server(module, context=context)
    server.bind(endpoint)
    gevent.spawn(server.run)

    client = zerorpc.Client(context=context)
    client.connect(endpoint)

    args = 1, 2, 3
    kwargs = {'a': 7, 'b': 8}
    res = client.echo(*args, **kwargs)
    assert len(res) == 2
    assert res[0] == args
    assert len(res[1]) == len(kwargs)
    assert 'a' in res[1] and 'b' in res[1]

    client.close()
    server.close()
def test_kwargs_pickle():
    class Srv(object):
        def echo(self, *args, **kwargs):
            return args, kwargs

    endpoint = random_ipc_endpoint()

    context = Context()
    context.register_serializer("pickle")

    module = Srv()
    server = zerorpc.Server(module, context=context)
    server.bind(endpoint)
    gevent.spawn(server.run)

    client = zerorpc.Client(context=context)
    client.connect(endpoint)

    args = 1, 2, 3
    kwargs = {'a': 7, 'b': 8, 'now': datetime.now()}
    res = client.echo(*args, **kwargs)
    assert len(res) == 2
    assert res[0] == args
    assert len(res[1]) == len(kwargs)
    assert 'a' in res[1] and 'b' in res[1] and isinstance(
        res[1]['now'], datetime)

    client.close()
    server.close()
def test_pickle_tuple():
    class Srv(object):
        def echo(self, *args):
            return args

    endpoint = random_ipc_endpoint()

    context = Context()
    context.register_serializer("pickle")

    module = Srv()
    server = zerorpc.Server(module, context=context)
    server.bind(endpoint)
    gevent.spawn(server.run)

    client = zerorpc.Client(context=context)
    client.connect(endpoint)

    args = (1, 2, 3)
    res = client.echo(*args)
    assert len(res) == 3
    assert res == args

    client.close()
    server.close()