FastAPI-Scheduler is a simple scheduled task management FastAPI extension based on APScheduler.
Project description
FastAPI-Scheduler
项目介绍
FastAPI-Scheduler
是一个基于APScheduler
的简单定时任务管理FastAPI
拓展库.
安装
pip install fastapi-scheduler
简单示例
main.py:
from fastapi import FastAPI
from fastapi_amis_admin.admin.settings import Settings
from fastapi_amis_admin.admin.site import AdminSite
from datetime import date
from fastapi_scheduler import SchedulerAdmin
# 创建`FastAPI`应用
app = FastAPI()
# 创建`AdminSite`实例
site = AdminSite(settings=Settings(database_url_async='sqlite+aiosqlite:///amisadmin.db'))
# # 自定义定时任务调度器
# from apscheduler.schedulers.asyncio import AsyncIOScheduler
# from apscheduler.jobstores.redis import RedisJobStore
# # 使用`RedisJobStore`创建任务存储
# scheduler = AsyncIOScheduler(jobstores={'default':RedisJobStore(db=2,host="127.0.0.1",port=6379,password="test")})
# scheduler = SchedulerAdmin.bind(site,scheduler=scheduler)
# 创建定时任务调度器`SchedulerAdmin`实例
scheduler = SchedulerAdmin.bind(site)
# 添加定时任务,参考官方文档: https://apscheduler.readthedocs.io/en/master/
# use when you want to run the job at fixed intervals of time
@scheduler.scheduled_job('interval', seconds=60)
def interval_task_test():
print('interval task is run...')
# use when you want to run the job periodically at certain time(s) of day
@scheduler.scheduled_job('cron', hour=3, minute=30)
def cron_task_test():
print('cron task is run...')
# use when you want to run the job just once at a certain point of time
@scheduler.scheduled_job('date', run_date=date(2022, 11, 11))
def date_task_test():
print('date task is run...')
# 挂载后台管理系统
site.mount_app(app)
@app.on_event("startup")
async def startup():
# 启动定时任务调度器
scheduler.start()
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, debug=True)
界面预览
- Open
http://127.0.0.1:8000/admin/
in your browser:
依赖项目
许可协议
该项目遵循 Apache2.0 许可协议。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for fastapi_scheduler-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4aa408c4e48a8cc41325d62a01174e11e6584ff8f01980513fb43b36c89dde9 |
|
MD5 | 4ddfd800105be99dd0a0db394e8673f0 |
|
BLAKE2b-256 | b74dfdb2ac269958a6c4dde37bdd785826e305360106b199d20f5da864f0eed9 |