Как выполнить асин c операции в обработчике запроса aiohttp после отправки ответа? - PullRequest
1 голос
/ 15 апреля 2020

Я хочу выполнить asyn c операции в обработчике запроса aiohttp после отправки ответа. Как за документы я должен написать

async def ping_handler(request):
    """Send PONG and increase DB counter."""

    # explicitly send the response
    resp = web.json_response({'message': 'PONG'})
    await resp.prepare(request)
    await resp.write_eof()

    # job after sending resp
    time.sleep(1)

    return resp

Этот рецепт отлично работает для не асинхронных c функций. Но когда я использую asyncio.sleep, он дает сбой:

async def ping_handler(request):
    """Send PONG and increase DB counter."""

    # explicitly send the response
    resp = web.json_response({'message': 'PONG'})
    await resp.prepare(request)
    await resp.write_eof()

    # job after sending resp
    print('Enter job')
    await asyncio.sleep(1)
    print('Exit job')

    return resp

Я вижу Enter job, но не Exit job.

Что я делаю не так?

...