小编jia*_*nyi的帖子

FastAPI中如何正确使用ApScheduler?

from fastapi import FastAPI\nfrom fastapi.middleware.cors import CORSMiddleware\nimport uvicorn\nimport time\nfrom loguru import logger\nfrom apscheduler.schedulers.background import BackgroundScheduler\n\napp = FastAPI()\napp.add_middleware(\n    CORSMiddleware,\n    allow_origins=["*"],\n    allow_credentials=True,\n    allow_methods=["*"],\n    allow_headers=["*"],\n)\n\ntest_list = ["1"]*10\n\ndef check_list_len():\n    global test_list\n    while True:\n        time.sleep(5)\n        logger.info(f"check_list_len\xef\xbc\x9a{len(test_list)}")\n\n@app.on_event('startup')\ndef init_data():\n    scheduler = BackgroundScheduler()\n    scheduler.add_job(check_list_len, 'cron', second='*/5')\n    scheduler.start()\n\n@app.get("/pop")\nasync def list_pop():\n    global test_list\n    test_list.pop(1)\n    logger.info(f"current_list_len:{len(test_list)}")\n\n\nif __name__ == '__main__':\n    uvicorn.run(app="main3:app", host="0.0.0.0", port=80, reload=False, debug=False)\n
Run Code Online (Sandbox Code Playgroud)\n

上面是我的代码,我想通过get请求取出一个列表元素,并设置一个周期性任务不断检查列表中的元素数量,但是当我运行时,总是出现以下错误:

\n
Execution of job "check_list_len (trigger: cron[second='*/5'], next run at: 2021-11-25 09:48:50 CST)" skipped: maximum number of running instances reached (1)\n2021-11-25 09:48:50.016 | INFO     | …
Run Code Online (Sandbox Code Playgroud)

python-3.x apscheduler fastapi uvicorn

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

apscheduler ×1

fastapi ×1

python-3.x ×1

uvicorn ×1