推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
monetto
V2EX  ›  Python

flask 类似于 Spring ApplicationListener 的逻辑吗?

  •  
  •   monetto · Oct 28, 2019 · 3274 views
    This topic created in 2527 days ago, the information mentioned may be changed or developed.

    如题,想在 Flask 应用启动完成后进行一些初始化逻辑,Spring 可以使用 ApplicationListener,但是 Flask 没有查到,有大佬知道的吗?

    6 replies  •  2019-10-29 11:02:05 +08:00
    ClericPy
        1
    ClericPy  
       Oct 29, 2019
    没见过... 只知道有个第一次请求前的回调 before_app_first_request -> before_first_request_funcs. 反正 signals 里没看到
    Sanic 和 starlette 那些协程库倒是可以添加 after_server_start 事件绑定...
    什么任务不能初始化 App 的时候直接做好准备, 再去启动...
    monetto
        2
    monetto  
    OP
       Oct 29, 2019
    @ClericPy 比如在 Flask 应用启动前需要缓存进 Redis 一些数据,这一步骤希望在 Flask 启动完成后执行
    cz5424
        3
    cz5424  
       Oct 29, 2019 via iPhone
    @monetto 使用工厂模式,注册蓝图之后去初始化你要的东西
    ClericPy
        4
    ClericPy  
       Oct 29, 2019
    @monetto 看这个意思是... flask 初始化 app 还不够, 还要启动, 并且用户发来请求之前, 那 before_app_first_request 然后自己 Requests 自己不够么, 写个能阻塞的就够了吧. 这还不行只能去 flask 源码里找钩子了... 你要的 startup event listener 我至少在 stackoverflow 和 flask 官方 Github 都没看到
    monetto
        5
    monetto  
    OP
       Oct 29, 2019
    @ClericPy 好的(#捂脸)
    monetto
        6
    monetto  
    OP
       Oct 29, 2019
    @cz5424 我看看去,3Q
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3776 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 05:31 · PVG 13:31 · LAX 22:31 · JFK 01:31
    ♥ Do have faith in what you're doing.