在Django中,views.py
主要用于处理HTTP请求并返回响应。为了确保定时任务不会干扰到HTTP请求的处理,你需要将定时任务的执行与请求处理分离。下面是如何在views.py
中实现这一点,并确保定时任务不会阻塞请求处理流程的一些示例。
# views.py
import time, json
from apscheduler.schedulers.background import BackgroundScheduler
from app_demo.tasks import *
# 采用非阻塞的方式
scheduler = BackgroundScheduler()
scheduler.add_job