Как ждать сопрограмму с циклом событий - PullRequest
1 голос
/ 18 апреля 2020

У меня есть сопрограмма с событием l oop. Как мне ждать эту сопрограмму? Я использую Python 3.7.3.

# The coroutine
async def main():
    #
    # This code written using apscheduler.schedulers.asyncio.AsyncIOScheduler
    # which has its *event loop* to run scheduled tasks
    # 
    # However, this code will await another coroutine, e.g.,
    # redis = await aioredis.create_redis('/tmp/redis.sock')

    try:
        asyncio.get_event_loop().run_forever()
    except:
        pass


if __name__ == '__main__':
    asyncio.run(main())

Код запускает только основное событие l oop, созданное asyncio.run(), но не управляемое apscheduler.

Обновление 1

Однако, если изменить код на

# The coroutine
async def main():
    #
    # This code written using apscheduler.schedulers.asyncio.AsyncIOScheduler
    # which has its *event loop* to run scheduled tasks
    # 
    # However, this code will await another coroutine, e.g.,
    # redis = await aioredis.create_redis('/tmp/redis.sock')

    #try:
    #    asyncio.get_event_loop().run_forever()
    #except:
    #    pass


if __name__ == '__main__':
    asyncio.run(main())

Код запускается main() один раз, так как событие l oop, управляемое apscheduler, не запускается .

Однако, если изменить код на

# The coroutine
async def main():
    #
    # This code written using apscheduler.schedulers.asyncio.AsyncIOScheduler
    # which has its *event loop* to run scheduled tasks
    # 
    # However, this code will await another coroutine, e.g.,
    # redis = await aioredis.create_redis('/tmp/redis.sock')

    try:
        asyncio.get_event_loop().run_forever()
    except:
        pass


if __name__ == '__main__':
    main()

Жалуется, что сопрограмма 'main' никогда не ожидалась.

...