Пример #1
0
async def exit_signal_with_service(
        service_to_exit: BaseService) -> AsyncGenerator[None, None]:
    loop = service_to_exit.get_event_loop()
    async with exit_signal(loop):
        await service_to_exit.cancel()
        yield
        service_to_exit._executor.shutdown(wait=True)
Пример #2
0
async def exit_on_signal(service_to_exit: BaseService) -> None:
    loop = service_to_exit.get_event_loop()
    sigint_received = asyncio.Event()
    for sig in [signal.SIGINT, signal.SIGTERM]:
        # TODO also support Windows
        loop.add_signal_handler(sig, sigint_received.set)

    await sigint_received.wait()
    try:
        await service_to_exit.cancel()
    finally:
        loop.stop()
Пример #3
0
async def exit_on_signal(service_to_exit: BaseService,
                         endpoint: Endpoint = None) -> None:
    loop = service_to_exit.get_event_loop()
    sigint_received = asyncio.Event()
    for sig in [signal.SIGINT, signal.SIGTERM]:
        # TODO also support Windows
        loop.add_signal_handler(sig, sigint_received.set)

    await sigint_received.wait()
    try:
        await service_to_exit.cancel()
        if endpoint is not None:
            endpoint.stop()
        service_to_exit._executor.shutdown(wait=True)
    finally:
        loop.stop()
Пример #4
0
def run_service_until_quit(service: BaseService) -> None:
    loop = service.get_event_loop()
    asyncio.ensure_future(exit_on_signal(service), loop=loop)
    asyncio.ensure_future(service.run(), loop=loop)
    loop.run_forever()
    loop.close()