AttributeError: module 'apscheduler.schedulers.asyncio' has no attribute 'get_event_loop'

AttributeError: module 'apscheduler.schedulers.asyncio' has no attribute 'get_event_loop'

我正在尝试实现时钟进程以在 Heroku dyno 中使用。我正在使用 Python 3.6。时钟进程将 运行 每 3 小时。这是代码:

import os
import sys

import requests
from apscheduler.schedulers import asyncio
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.interval import IntervalTrigger
from webdriverdownloader import GeckoDriverDownloader

from scraper.common import main


def get_driver():
    return True

def notify(str):
    return True;


if __name__ == '__main__':
    scheduler = AsyncIOScheduler()
    get_driver()
    scheduler.add_job(main, trigger=IntervalTrigger(hours=3))

    scheduler.start()
    # Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed.
    try:
        loop = asyncio.get_event_loop()
        loop.run_until_complete(asyncio.wait())
    except (KeyboardInterrupt, SystemExit):
        pass

起初我试过

asyncio.get_event_loop().run_forever()

但是,我了解到 python 3.6 不支持此功能,因此我将此声明更改为 run_until_complete

如果我运行这个例子,代码打印出来:

AttributeError: module 'apscheduler.schedulers.asyncio' has no attribute 'get_event_loop'

有谁知道为什么会出现这个错误?任何帮助将非常感激。提前致谢!

您不是从标准库中导入 asyncio 模块,而是从 apscheduler 库中导入 asyncio 模块。您可以通过访问 link here.

来查看

您只能从该命名空间导入两件事:

  1. run_in_event_loop
  2. AsyncIOScheduler

如果您需要使用低级 asyncio API 只需直接从标准库中导入 asyncio