How to use ApScheduler correctly in FastAPI?
Asked Answered
B

1

7
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import time
from loguru import logger
from apscheduler.schedulers.background import BackgroundScheduler

app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

test_list = ["1"]*10

def check_list_len():
    global test_list
    while True:
        time.sleep(5)
        logger.info(f"check_list_len:{len(test_list)}")

@app.on_event('startup')
def init_data():
    scheduler = BackgroundScheduler()
    scheduler.add_job(check_list_len, 'cron', second='*/5')
    scheduler.start()

@app.get("/pop")
async def list_pop():
    global test_list
    test_list.pop(1)
    logger.info(f"current_list_len:{len(test_list)}")


if __name__ == '__main__':
    uvicorn.run(app="main3:app", host="0.0.0.0", port=80, reload=False, debug=False)

Above is my code, I want to take out a list of elements through get request, and set a periodic task constantly check the number of elements in the list, but when I run, always appear the following error:

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)
2021-11-25 09:48:50.016 | INFO     | main3:check_list_len:23 - check_list_len:10
Execution of job "check_list_len (trigger: cron[second='*/5'], next run at: 2021-11-25 09:48:55 CST)" skipped: maximum number of running instances reached (1)
2021-11-25 09:48:55.018 | INFO     | main3:check_list_len:23 - check_list_len:10
INFO:     127.0.0.1:55961 - "GET /pop HTTP/1.1" 200 OK
2021-11-25 09:48:57.098 | INFO     | main3:list_pop:35 - current_list_len:9
Execution of job "check_list_len (trigger: cron[second='*/5'], next run at: 2021-11-25 09:49:00 CST)" skipped: maximum number of running instances reached (1)
2021-11-25 09:49:00.022 | INFO     | main3:check_list_len:23 - check_list_len:9

It looks like I started two scheduled tasks and only one succeeded, but I started only one task. How do I avoid this

Biaxial answered 25/11, 2021 at 2:1 Comment(0)
J
8

You're getting the behavior you're asking for. You've configured apscheduler to run check_list_len every five seconds, but you've also made it so that function runs without terminating - just sleeping for five seconds in an endless loop. That function never terminates, so apscheduler doesn't run it again - since it still hasn't finished.

Remove the infinite loop inside your utility function when using apscheduler - it'll call the function every five seconds for you:

def check_list_len():
    global test_list  # you really don't need this either, since you're not reassigning the variable
    logger.info(f"check_list_len:{len(test_list)}")
Jayson answered 25/11, 2021 at 9:31 Comment(1)
Thank you very much for your answer. I thought it was a framework problem, but it was a low-level code problem. I'm sorry.@JaysonBiaxial

© 2022 - 2024 — McMap. All rights reserved.