【问题标题】:Is there a way to cancel the start depending on a condition with FastAPI startup event?有没有办法根据 FastAPI 启动事件的条件取消启动?
【发布时间】:2021-07-27 21:09:27
【问题描述】:

我要做的是检查启动事件的条件,如果发生执行,请不要启动服务器或停止服务器。


@app.on_event("startup")
def startup_event():
    public_key = None

    try:
        with open(PUBLIC_KEY_FILE) as public_key_file:
            public_key = public_key_file.read()
    except Exception as f_error:
        logger.exception(f_error)

        # cancel the startup

有办法吗?

【问题讨论】:

    标签: startup fastapi starlette


    【解决方案1】:

    在启动函数中引发异常:

    
    @app.on_event("startup")
    def startup_event():
        public_key = None
    
        try:
            with open(PUBLIC_KEY_FILE) as public_key_file:
                public_key = public_key_file.read()
        except Exception as f_error:
            logger.exception(f_error)
    
            raise SomeException()
    

    【讨论】:

      猜你喜欢
      • 2022-09-24
      • 2021-09-26
      • 2020-03-07
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2020-08-11
      相关资源
      最近更新 更多