V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
sunhk25
V2EX  ›  Python

Flask 通过子域名启动多应用 APP(应用间想独立配置),切换时响应时间很慢

  •  
  •   sunhk25 · Feb 29, 2020 · 2272 views
    This topic created in 2248 days ago, the information mentioned may be changed or developed.

    一个 WB 系统下有多个应用,有些设置是共通的,有些需要按应用单独配置。

    • 方案 1:通过子域名管理多应用 APP,每个应用都有自己的 current_app,找到了下面的示例。 但是在「 dev 」和「 qa 」间却换时,非常耗时。难道多应用进程会很消耗吗?

    https://github.com/saltycrane/flask-subdomain-dispatcher-example

    • 方案 2:通过蓝图的来 subdomain,子域名间切换很快,但是配置文件如何动态分配到 current_app 呢? 是不应该在 before_request 里面加一个子域名跟应用的判断,然后更新到 current_app ?

    app.register_blueprint(dev_bp, subdomain='dev')

    4 replies    2020-03-01 11:19:33 +08:00
    encro
        1
    encro  
       Feb 29, 2020
    没看,你的方法是重新 init app 吧,register_blueprint 还是当前 app。
    encro
        2
    encro  
       Feb 29, 2020
    如果的配置牵涉到底层比如数据库切换,就需要自己分装下 flash 的 db 扩张了。
    如果不设计,为什么不实现自己的 config 类呢。

    在这里问,不如自己看源码快。
    sunhk25
        3
    sunhk25  
    OP
       Feb 29, 2020
    @encro
    - 方案一就是根据子域调度,请求后不存在的话初始化一个 app。只有一个子域时速度没问题。
    ```python
    def _get_application(self, host):
    print("HTTP_HOST", host, self.domain,host.endswith(self.domain))
    host = host.split(':')[0]
    assert host.endswith(self.domain), f'Configuration error.{host},{self.domain}'
    subdomain = host[:-len(self.domain)].rstrip('.')
    with self.lock:
    app = self.instances.get(subdomain)
    if app is None:
    configobj = self._get_subdomain_based_config(subdomain)
    app = self.create_app(
    configobj=configobj, *self.args, **self.kwargs)
    self.instances[subdomain] = app
    return app
    ```
    - 有自己的 config 类、应用配置 AppConfig 继承共通 BaseConfig。然后想向通过 curren_app 轻松读取配置
    encro
        4
    encro  
       Mar 1, 2020
    很久以前 flask 弄过支持多域名的了,不过是所有域名公用一个 app,你这个是多个 APP,肯定速度上不如一个块,内存占用上就会大很多,比较浪费,除非每个实例代码不一样,否则不建议这样做。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   874 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 22:02 · PVG 06:02 · LAX 15:02 · JFK 18:02
    ♥ Do have faith in what you're doing.