def ThriftAsyncServerFactory(
    processor, interface=None, port=0, loop=None, nthreads=None, sock=None,
    backlog=100
):
    if loop is None:
        loop = asyncio.get_event_loop()

    if not isinstance(processor, TProcessor):
        try:
            processor = processor._processor_type(processor, loop=loop)
        except AttributeError:
            raise TypeError(
                "Unsupported processor type: {}".format(type(processor)),
            )

    if nthreads:
        from concurrent.futures import ThreadPoolExecutor
        loop.set_default_executor(
            ThreadPoolExecutor(max_workers=nthreads),
        )
    event_handler = TServerEventHandler()
    pfactory = ThriftServerProtocolFactory(processor, event_handler, loop)
    server = yield From(loop.create_server(
        pfactory,
        interface,
        port,
        sock=sock,
        backlog=backlog,
    ))

    if server.sockets:
        for socket in server.sockets:
            event_handler.preServe(socket.getsockname())

    raise Return(server)
Example #2
0
def ThriftAsyncServerFactory(processor,
                             interface=None,
                             port=0,
                             loop=None,
                             nthreads=None,
                             sock=None,
                             backlog=100,
                             event_handler=None):
    if loop is None:
        loop = asyncio.get_event_loop()

    if not isinstance(processor, TProcessor):
        try:
            processor = processor._processor_type(processor, loop=loop)
        except AttributeError:
            raise TypeError(
                "Unsupported processor type: {}".format(type(processor)), )

    if nthreads:
        from concurrent.futures import ThreadPoolExecutor
        loop.set_default_executor(ThreadPoolExecutor(max_workers=nthreads), )

    ehandler = TServerEventHandler(
    ) if event_handler is None else event_handler
    pfactory = ThriftServerProtocolFactory(processor, ehandler, loop)
    server = yield From(
        loop.create_server(
            pfactory,
            interface,
            port,
            sock=sock,
            backlog=backlog,
        ))

    if server.sockets:
        for socket in server.sockets:
            ehandler.preServe(socket.getsockname())

    raise Return(server)
Example #3
0
def ThriftAsyncServerFactory(processor,
                             *,
                             interface=None,
                             port=0,
                             loop=None,
                             nthreads=None,
                             sock=None,
                             backlog=100):
    """
    ThriftAsyncServerFactory(processor) -> asyncio.Server

    asyncio.Server factory for Thrift processors. In the spirit of "there
    should be one obvious way to do it", this server only supports the new
    THeader protocol.

    If `interface` is None (the default), listens on all interfaces. `port` is
    0 by default, which makes the OS allocate the port. Enumerate the returned
    server's "sockets" attribute to know the port in this case.

    If not given, the default event loop is used. If `nthreads` is given, the
    default executor for the event loop is set to a thread pool of up to
    `nthreads`.

    Notes about the processor method handling:

    1. By default all methods are executed synchronously on the event loop.
       This can lead to poor performance if a single run takes long to process.

    2. Mark coroutines with @asyncio.coroutine if you wish to use "yield from"
       to call async services, schedule tasks with customized executors, etc.

    3. Mark methods with @run_on_thread if you wish to run them on the thread
       pool executor. Note that unless you're accessing C extensions which free
       the GIL, this is not going to win you any performance.

    Use this to initialize multiple servers asynchronously::

        loop = asyncio.get_event_loop()
        servers = [
            ThriftAsyncServerFactory(handler1, port=9090, loop=loop),
            ThriftAsyncServerFactory(handler2, port=9091, loop=loop),
        ]
        loop.run_until_complete(asyncio.wait(servers))
        try:
            loop.run_forever()   # Servers are initialized now
        finally:
            for server in servers:
                server.close()
    """

    if loop is None:
        loop = asyncio.get_event_loop()

    if not isinstance(processor, TProcessor):
        try:
            processor = processor._processor_type(processor, loop=loop)
        except AttributeError:
            raise TypeError(
                "Unsupported processor type: {}".format(type(processor)), )

    if nthreads:
        from concurrent.futures import ThreadPoolExecutor
        loop.set_default_executor(ThreadPoolExecutor(max_workers=nthreads), )
    event_handler = TServerEventHandler()
    pfactory = ThriftServerProtocolFactory(processor, event_handler, loop)
    server = yield from loop.create_server(
        pfactory,
        interface,
        port,
        sock=sock,
        backlog=backlog,
    )

    if server.sockets:
        for socket in server.sockets:
            event_handler.preServe(socket.getsockname())

    return server
Example #4
0
 def _create_server_event_handler(self):
     return TServerEventHandler()
Example #5
0
async def ThriftAsyncServerFactory(
    processor,
    *,
    interface=None,
    port=0,
    loop=None,
    nthreads=None,
    sock=None,
    backlog=100,
    ssl=None,
    event_handler=None,
    protocol_factory=None,
):
    """
    ThriftAsyncServerFactory(processor) -> asyncio.Server

    asyncio.Server factory for Thrift processors. In the spirit of "there
    should be one obvious way to do it", this server only supports the new
    THeader protocol.

    If `interface` is None (the default), listens on all interfaces. `port` is
    0 by default, which makes the OS allocate the port. Enumerate the returned
    server's "sockets" attribute to know the port in this case.

    If not given, the default event loop is used. If `nthreads` is given, the
    default executor for the event loop is set to a thread pool of up to
    `nthreads`.

    ssl is an instance of ssl.SSLContext. If None (default) or False SSL/TLS is
    not used.

    event_handler must be a subclass of thrift.server.TServer. If None,
    thrift.server.TServer.TServerEventHandler is used. Specify a custom handler
    for custom event handling (e.g. handling new connections)

    protocol_factory is a function that takes a triplet of
    (processor, event_handler, loop=None) and returns a `asyncio.Protocol` instance
    that will be passed to a call to `asyncio.create_server`. processor will be a
    subclass of `TProcessor`, event_handler will be a subclass of `TServer`, and
    loop is an `Optional[asyncio.AbstractEventLoop]`. If protocol_factory is None
    `ThriftHeaderServerProtocol` is used.

    Notes about the processor method handling:

    1. By default all methods are executed synchronously on the event loop.
       This can lead to poor performance if a single run takes long to process.

    2. Mark coroutines with `async def` if you wish to use `await`
       to call async services, schedule tasks with customized executors, etc.

    3. Mark methods with @run_on_thread if you wish to run them on the thread
       pool executor. Note that unless you're accessing C extensions which free
       the GIL, this is not going to win you any performance.

    Use this to initialize multiple servers asynchronously::

        loop = asyncio.get_event_loop()
        servers = [
            ThriftAsyncServerFactory(handler1, port=9090, loop=loop),
            ThriftAsyncServerFactory(handler2, port=9091, loop=loop),
        ]
        loop.run_until_complete(asyncio.wait(servers))
        try:
            loop.run_forever()   # Servers are initialized now
        finally:
            for server in servers:
                server.close()
    """

    if loop is None:
        loop = asyncio.get_event_loop()

    if not isinstance(processor, TProcessor):
        try:
            processor = processor._processor_type(processor, loop=loop)
        except AttributeError:
            raise TypeError(
                "Unsupported processor type: {}".format(type(processor)), )

    if nthreads:
        from concurrent.futures import ThreadPoolExecutor

        loop.set_default_executor(ThreadPoolExecutor(max_workers=nthreads), )
    ehandler = TServerEventHandler(
    ) if event_handler is None else event_handler
    protocol_factory = protocol_factory or ThriftHeaderServerProtocol
    pfactory = functools.partial(protocol_factory, processor, ehandler, loop)
    server = await loop.create_server(
        pfactory,
        interface,
        port,
        sock=sock,
        backlog=backlog,
        ssl=ssl,
    )

    if server.sockets:
        for socket in server.sockets:
            ehandler.preServe(socket.getsockname())

    return server
Example #6
0
async def ThriftAsyncServerFactory(
    processor, *, interface=None, port=0, loop=None, nthreads=None, sock=None,
    backlog=100, ssl=None, event_handler=None, protocol_factory=None
):
    """
    ThriftAsyncServerFactory(processor) -> asyncio.Server

    asyncio.Server factory for Thrift processors. In the spirit of "there
    should be one obvious way to do it", this server only supports the new
    THeader protocol.

    If `interface` is None (the default), listens on all interfaces. `port` is
    0 by default, which makes the OS allocate the port. Enumerate the returned
    server's "sockets" attribute to know the port in this case.

    If not given, the default event loop is used. If `nthreads` is given, the
    default executor for the event loop is set to a thread pool of up to
    `nthreads`.

    ssl is an instance of ssl.SSLContext. If None (default) or False SSL/TLS is
    not used.

    event_handler must be a subclass of thrift.server.TServer. If None,
    thrift.server.TServer.TServerEventHandler is used. Specify a custom handler
    for custom event handling (e.g. handling new connections)

    protocol_factory is a function that takes a triplet of
    (processor, event_handler, loop=None) and returns a `asyncio.Protocol` instance
    that will be passed to a call to `asyncio.create_server`. processor will be a
    subclass of `TProcessor`, event_handler will be a subclass of `TServer`, and
    loop is an `Optional[asyncio.AbstractEventLoop]`. If protocol_factory is None
    `ThriftHeaderServerProtocol` is used.

    Notes about the processor method handling:

    1. By default all methods are executed synchronously on the event loop.
       This can lead to poor performance if a single run takes long to process.

    2. Mark coroutines with `async def` if you wish to use `await`
       to call async services, schedule tasks with customized executors, etc.

    3. Mark methods with @run_on_thread if you wish to run them on the thread
       pool executor. Note that unless you're accessing C extensions which free
       the GIL, this is not going to win you any performance.

    Use this to initialize multiple servers asynchronously::

        loop = asyncio.get_event_loop()
        servers = [
            ThriftAsyncServerFactory(handler1, port=9090, loop=loop),
            ThriftAsyncServerFactory(handler2, port=9091, loop=loop),
        ]
        loop.run_until_complete(asyncio.wait(servers))
        try:
            loop.run_forever()   # Servers are initialized now
        finally:
            for server in servers:
                server.close()
    """

    if loop is None:
        loop = asyncio.get_event_loop()

    if not isinstance(processor, TProcessor):
        try:
            processor = processor._processor_type(processor, loop=loop)
        except AttributeError:
            raise TypeError(
                "Unsupported processor type: {}".format(type(processor)),
            )

    if nthreads:
        from concurrent.futures import ThreadPoolExecutor
        loop.set_default_executor(
            ThreadPoolExecutor(max_workers=nthreads),
        )
    ehandler = TServerEventHandler() if event_handler is None else event_handler
    protocol_factory = protocol_factory or ThriftHeaderServerProtocol
    pfactory = functools.partial(protocol_factory, processor, ehandler, loop)
    server = await loop.create_server(
        pfactory,
        interface,
        port,
        sock=sock,
        backlog=backlog,
        ssl=ssl,
    )

    if server.sockets:
        for socket in server.sockets:
            ehandler.preServe(socket.getsockname())

    return server
def ThriftAsyncServerFactory(
    processor, *, interface=None, port=0, loop=None, nthreads=None, sock=None,
    backlog=100
):
    """
    ThriftAsyncServerFactory(processor) -> asyncio.Server

    asyncio.Server factory for Thrift processors. In the spirit of "there
    should be one obvious way to do it", this server only supports the new
    THeader protocol.

    If `interface` is None (the default), listens on all interfaces. `port` is
    0 by default, which makes the OS allocate the port. Enumerate the returned
    server's "sockets" attribute to know the port in this case.

    If not given, the default event loop is used. If `nthreads` is given, the
    default executor for the event loop is set to a thread pool of up to
    `nthreads`.

    Notes about the processor method handling:

    1. By default all methods are executed synchronously on the event loop.
       This can lead to poor performance if a single run takes long to process.

    2. Mark coroutines with @asyncio.coroutine if you wish to use "yield from"
       to call async services, schedule tasks with customized executors, etc.

    3. Mark methods with @run_on_thread if you wish to run them on the thread
       pool executor. Note that unless you're accessing C extensions which free
       the GIL, this is not going to win you any performance.

    Use this to initialize multiple servers asynchronously::

        loop = asyncio.get_event_loop()
        servers = [
            ThriftAsyncServerFactory(handler1, port=9090, loop=loop),
            ThriftAsyncServerFactory(handler2, port=9091, loop=loop),
        ]
        loop.run_until_complete(asyncio.wait(servers))
        try:
            loop.run_forever()   # Servers are initialized now
        finally:
            for server in servers:
                server.close()
    """

    if loop is None:
        loop = asyncio.get_event_loop()

    if not isinstance(processor, TProcessor):
        try:
            processor = processor._processor_type(processor, loop=loop)
        except AttributeError:
            raise TypeError(
                "Unsupported processor type: {}".format(type(processor)),
            )

    if nthreads:
        from concurrent.futures import ThreadPoolExecutor
        loop.set_default_executor(
            ThreadPoolExecutor(max_workers=nthreads),
        )
    event_handler = TServerEventHandler()
    pfactory = ThriftServerProtocolFactory(processor, event_handler, loop)
    server = yield from loop.create_server(
        pfactory,
        interface,
        port,
        sock=sock,
        backlog=backlog,
    )

    if server.sockets:
        for socket in server.sockets:
            event_handler.preServe(socket.getsockname())

    return server