RuntimeError: There is no current event loop in thread in async + apscheduler

前端 未结 5 1948
我寻月下人不归
我寻月下人不归 2020-12-12 21:00

I have a async function and need to run in with apscheduller every N minutes. There is a python code below

URL_LIST = [\'\',
            \'

        
5条回答
  •  旧巷少年郎
    2020-12-12 21:33

    Use asyncio.run() instead of directly using the event loop. It creates a new loop and closes it when finished.

    This is how the 'run' looks like:

    if events._get_running_loop() is not None:
        raise RuntimeError(
            "asyncio.run() cannot be called from a running event loop")
    
    if not coroutines.iscoroutine(main):
        raise ValueError("a coroutine was expected, got {!r}".format(main))
    
    loop = events.new_event_loop()
    try:
        events.set_event_loop(loop)
        loop.set_debug(debug)
        return loop.run_until_complete(main)
    finally:
        try:
            _cancel_all_tasks(loop)
            loop.run_until_complete(loop.shutdown_asyncgens())
        finally:
            events.set_event_loop(None)
            loop.close()
    

提交回复
热议问题