gunicorn+flask, 不同的 worker 之间怎么共享一个对象 - V2EX
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
unfurl

gunicorn+flask, 不同的 worker 之间怎么共享一个对象

  •  
  •   unfurl Aug 18, 2016 10951 views
    This topic created in 3549 days ago, the information mentioned may be changed or developed.

    我用 flask 实现了两个接口,用于操作一个字典对象,伪代码如下:

    # -*- coding: utf-8 -*- from flask import Flask, request app = Flask(__name__) shared_dict = dict() @app.route("/set") def set(): shared_dict[...] = request... @app.route("/get") def get(): return shared_dict[...] 

    直接用app.run()这个程序没有任何问题

    现在想要部署用gunicorn+flask的方式部署,启动参数gunicorn -w 4 main:app

    我发现不同的 worker 不是共用这个shared_dict对象,翻查了很多文档,没有找到解决办法

    10 replies    2018-01-29 14:07:26 +08:00
    binux
        1
    binux  
       Aug 18, 2016   2
    设计时你就应该想好这个问题。取决于需求和访问量

    1. 使用 thread worker 来跑
    2. 使用外部资源共享对象,例如数据库,文件等
    3. 只跑一个 worker
    unfurl
        2
    unfurl  
    OP
       Aug 18, 2016
    @binux 设计时访问量不大,直接跑就行了,现在想用 gunicorn 部署,才发现这个问题,尴尬了
    paulw54jrn
        3
    paulw54jrn  
       Aug 18, 2016
    这个涉及到操作系统进程的基本概念...
    felixzhu
        4
    felixzhu  
       Aug 18, 2016
    引入一个外部存储不就行了。开个 redis 啥的
    unfurl
        5
    unfurl  
    OP
       Aug 18, 2016
    @felixzhu 我想先从代码层次来解决,实在不行只能上 redis 了
    josephok
        6
    josephok  
       Aug 18, 2016 via Android
    需要进程间通信了。
    lvhuiyang
        7
    lvhuiyang  
       Aug 18, 2016
    redis 正解
    lovepython
        8
    lovepython  
       Aug 19, 2016
    基本进程间通信的可能性不大了,因为是 gun 控制的,
    一楼的回答已经是最好的答案了
    wuyu1998
        9
    wuyu1998  
       Aug 20, 2016
    用 NoSQL 存放 shared_dict 对象的序列化字符串。
    alsoran1
        10
    alsoran1  
       Jan 29, 2018
    请问楼主 这个问题当时怎么解决的?有用到共享内存吗?
    About     Help     Advertise     Blog     API     FAQ     Solana     1150 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 41ms UTC 23:18 PVG 07:18 LAX 16:18 JFK 19:18
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86