石景山区网站建设,网线制作方法及步骤,中建五局华东建设公司网站,为什么网站用静态页面参考#xff1a;
中文文档#xff1a;FastAPI
API接口开发其实特简单#xff0c;Python FastApi Web 框架教程来了_根据接口文档用fastapi开发接口-CSDN博客
Python框架篇(1):FastApi-快速入门 - 知乎 官方示例目录结构
更大的应用 - 多个文件 - FastAPI
.
├── app …参考
中文文档FastAPI
API接口开发其实特简单Python FastApi Web 框架教程来了_根据接口文档用fastapi开发接口-CSDN博客
Python框架篇(1):FastApi-快速入门 - 知乎 官方示例目录结构
更大的应用 - 多个文件 - FastAPI
.
├── app # 「app」是一个 Python 包
│ ├── __init__.py # 这个文件使「app」成为一个 Python 包
│ ├── main.py # 「main」模块例如 import app.main
│ ├── dependencies.py # 「dependencies」模块例如 import app.dependencies
│ └── routers # 「routers」是一个「Python 子包」
│ │ ├── __init__.py # 使「routers」成为一个「Python 子包」
│ │ ├── items.py # 「items」子模块例如 import app.routers.items
│ │ └── users.py # 「users」子模块例如 import app.routers.users
│ └── internal # 「internal」是一个「Python 子包」
│ ├── __init__.py # 使「internal」成为一个「Python 子包」
│ └── admin.py # 「admin」子模块例如 import app.internal.admin
定时器任务 main.py
import time
from typing import Union
from apscheduler.schedulers.background import BackgroundScheduler
from fastapi import FastAPI
import uvicorn
from contextlib import asynccontextmanager启动命令
uvicorn main:app --reload --port 8000#导出依赖
pip freeze requirements.txtapi文档地址
http://localhost:8080/docs
data_task BackgroundScheduler()
task_id data_task_1asynccontextmanager
async def lifespan(app: FastAPI):# 星期2——6 5:30 执行一次# scheduler.add_job(functask_function, triggercron, day_of_week1,2,3,4,6, hour5,minute30)data_task.add_job(functask_function, triggerinterval, seconds1,idtask_id)data_task.start()# print(启动前执行)yield# print(关闭后前执行)app FastAPI(lifespanlifespan)def task_function():print(fCurrent Time: {time.strftime(%Y-%m-%d %H:%M:%S, time.localtime())})app.get(/scheduler/stop)
def stop():data_task.pause_job(task_id)print(task_id is stop)return task_id is stopapp.get(/scheduler/start)
def start():data_task.resume_job(task_id)print(task_id is start)return task_id is start# 第二种启动方式
if __name__ __main__:uvicorn.run(appmain:app, hostlocalhost, port8000)
参考
APScheduler定时任务框架 - 星空看海 - 博客园
crontab 定时任务、Python 定时任务框架 apscheduler_crontab 定时 执行 python 脚本-CSDN博客