FastAPI框架插件
fastapi-plugins的Python项目详细描述
fastapi插件
FastAPI框架插件
变更
安装
pip install fastapi-plugins pip install fastapi-plugins[memcached] pip install fastapi-plugins[all]
插件
缓存
内存缓存
有效变量为
MEMCACHED_HOST
-Memcached服务器主机。在MEMCACHED_PORT
-Memcached服务器端口。默认值为11211
。在MEMCACHED_POOL_MINSIZE
-要在池中创建的最小可用连接数。默认值为1
。在MEMCACHED_POOL_SIZE
-池中要保持的最大连接数。默认值为10
。必须大于0
。None
不允许。在MEMCACHED_PRESTART_TRIES
-号码尝试连接到Memcached实例。在MEMCACHED_PRESTART_WAIT
-应用程序启动时连接失败之间等待的间隔(秒)。在
示例
^{pr2}$Redis
支持
- 单个实例
- 哨兵
有效变量为
REDIS_TYPE
redis
-单个Redis实例sentinel
-Redis集群
REDIS_URL
-连接到Redis服务器的URL。例子redis://user:password@localhost:6379/2
。支持协议redis://
,rediss://
(基于TLS的redis)和unix://
。在REDIS_HOST
-Redis服务器主机。在REDIS_PORT
-Redis服务器端口。默认值为6379
。在REDIS_PASSWORD
-服务器的Redis密码。在REDIS_DB
-Redis-db(从零开始的数字索引)。默认值是0
。在REDIS_CONNECTION_TIMEOUT
-Redis连接超时。默认值是2
。在REDIS_POOL_MINSIZE
-要在池中创建的最小可用连接数。默认值为1
。在REDIS_POOL_MAXSIZE
-池中要保留的最大连接数。默认值为10
。必须大于0
。None
不允许。在REDIS_SENTINELS
-列出Redis sentinel地址或元组。在REDIS_SENTINEL_MASTER
-哨兵配置中主服务器的名称。默认值是mymaster
。在REDIS_PRESTART_TRIES
-号码尝试连接到a Redis实例。在REDIS_PRESTART_WAIT
-应用程序启动时连接失败之间等待的间隔(秒)。在
示例
# run with `uvicorn demo_app:app`importtypingimportaioredisimportfastapiimportpydanticimportfastapi_pluginsclassAppSettings(OtherSettings,fastapi_plugins.RedisSettings):api_name:str=str(__name__)app=fastapi.FastAPI()config=AppSettings()@app.get("/")asyncdefroot_get(cache:aioredis.Redis=fastapi.Depends(fastapi_plugins.depends_redis),)->typing.Dict:returndict(ping=awaitcache.ping())@app.on_event('startup')asyncdefon_startup()->None:awaitfastapi_plugins.redis_plugin.init_app(app,config=config)awaitfastapi_plugins.redis_plugin.init()@app.on_event('shutdown')asyncdefon_shutdown()->None:awaitfastapi_plugins.redis_plugin.terminate()
Docker Compose-Redis示例
version:'3.7'services:redis:image:redisports:-"6379:6379"demo_fastapi_plugin:image:demo_fastapi_pluginenvironment:-REDIS_TYPE=redis-REDIS_HOST=redis-REDIS_PORT=6379ports:-"8000:8000"
Docker Compose-Redis Sentinel示例
version:'3.7'services:...redis-sentinel:ports:-"26379:26379"environment:-...links:-redis-master-redis-slavedemo_fastapi_plugin:image:demo_fastapi_pluginenvironment:-REDIS_TYPE=sentinel-REDIS_SENTINELS=redis-sentinel:26379ports:-"8000:8000"
调度程序
简单地将awaitable作业安排为任务。在
- long运行
async
函数(例如监视系统或事件的文件) - 优雅地取消生成的任务
有效变量为:
AIOJOBS_CLOSE_TIMEOUT
-取消任务前的超时(以秒为单位)。在AIOJOBS_LIMIT
-要执行的并发任务数。在AIOJOBS_PENDING_LIMIT
-挂起的作业数(等待fr执行)。在
# run with `uvicorn demo_app:app`import...importfastapi_pluginsclassAppSettings(OtherSettings,fastapi_plugins.RedisSettings,fastapi_plugins.SchedulerSettings):api_name:str=str(__name__)app=fastapi.FastAPI()config=AppSettings()@app.post("/jobs/schedule/<timeout>")asyncdefjob_post(timeout:int=fastapi.Query(...,title='the job sleep time'),cache:aioredis.Redis=fastapi.Depends(fastapi_plugins.depends_redis),scheduler:aiojobs.Scheduler=fastapi.Depends(fastapi_plugins.depends_scheduler),# @IgnorePep8)->str:asyncdefcoro(job_id,timeout,cache):awaitcache.set(job_id,'processing')try:awaitasyncio.sleep(timeout)iftimeout==8:raiseException('ugly error')exceptasyncio.CancelledError:awaitcache.set(job_id,'canceled')exceptException:awaitcache.set(job_id,'erred')else:awaitcache.set(job_id,'success')job_id=str(uuid.uuid4()).replace('-','')awaitcache.set(job_id,'pending')awaitscheduler.spawn(coro(job_id,timeout,cache))returnjob_id@app.get("/jobs/status/<job_id>")asyncdefjob_get(job_id:str=fastapi.Query(...,title='the job id'),cache:aioredis.Redis=fastapi.Depends(fastapi_plugins.depends_redis),)->typing.Dict:status=awaitcache.get(job_id)ifstatusisNone:raisefastapi.HTTPException(status_code=starlette.status.HTTP_404_NOT_FOUND,detail='Job %s not found'%job_id)returndict(job_id=job_id,status=status)@app.on_event('startup')asyncdefon_startup()->None:awaitfastapi_plugins.redis_plugin.init_app(app,config=config)awaitfastapi_plugins.redis_plugin.init()awaitfastapi_plugins.scheduler_plugin.init_app(app=app,config=config)awaitfastapi_plugins.scheduler_plugin.init()@app.on_event('shutdown')asyncdefon_shutdown()->None:awaitfastapi_plugins.scheduler_plugin.terminate()awaitfastapi_plugins.redis_plugin.terminate()
。。。更多正在进行中…
发展
欢迎通过Issues
许可证
这个项目是根据麻省理工学院的许可条款授权的。在
变更
0.6.0(2020年11月26日)
[feature]
内存缓存
0.5.0(2020年11月25日)
0.4.2(2020年11月24日)
- [错误]修复Redis URL(#4)。在
0.4.1(2020-06-16)
- 重构要求
0.4.0(2020-04-09)
- 对
extra
的结构和拆分依赖关系
0.3.0(2020-04-07)
- 调度器:基于
aiojobs
的任务调度器
0.2.1(2020-04-06)
- Redis:预启动
0.2.0(2019-12-11)
- 雷迪斯:哨兵
0.1.0(2019-2011年-20)
- 初始版本:简单redis池客户端
- 项目
标签: