V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2020-02-29 15:07:24 +08:00 · 1836 次点击
    这是一个创建于 1508 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个 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 条回复    2020-03-01 11:19:33 +08:00
    encro
        1
    encro  
       2020-02-29 17:30:07 +08:00
    没看,你的方法是重新 init app 吧,register_blueprint 还是当前 app。
    encro
        2
    encro  
       2020-02-29 17:32:03 +08:00
    如果的配置牵涉到底层比如数据库切换,就需要自己分装下 flash 的 db 扩张了。
    如果不设计,为什么不实现自己的 config 类呢。

    在这里问,不如自己看源码快。
    sunhk25
        3
    sunhk25  
    OP
       2020-02-29 20:31:22 +08:00
    @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  
       2020-03-01 11:19:33 +08:00
    很久以前 flask 弄过支持多域名的了,不过是所有域名公用一个 app,你这个是多个 APP,肯定速度上不如一个块,内存占用上就会大很多,比较浪费,除非每个实例代码不一样,否则不建议这样做。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1300 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:27 · PVG 07:27 · LAX 16:27 · JFK 19:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.