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
ufo22940268
V2EX  ›  Python

gunicorn能够让一个单线程的服务器相应多个请求吗

  •  
  •   ufo22940268 ·
    ufo22940268 · 2013-09-24 15:59:06 +08:00 · 4339 次点击
    这是一个创建于 3872 天前的主题,其中的信息可能已经有所发展或是发生改变。


    比如说这么一段代码,我希望每一个请求过来之后能够马上打印出`request`. 这样子能够通过gunicorn做到吗?我试过`gunicorn -k gevent -w 4 a:app",貌似没有成功。

    如果不可以的话有什么好的解决方案吗?

    我现在正在用flask开发一个web应用,因为实在是菜鸟,所以很多名词用法不正确,望指出,谢谢
    7 条回复    1970-01-01 08:00:00 +08:00
    julyclyde
        1
    julyclyde  
       2013-09-24 16:16:57 +08:00
    print当然不会给浏览器
    ufo22940268
        2
    ufo22940268  
    OP
       2013-09-24 16:18:19 +08:00
    @julyclyde 我希望再终端下答应出来
    sobigfish
        3
    sobigfish  
       2013-09-24 16:45:10 +08:00
    logging貌似可以 试试?
    lqs
        4
    lqs  
       2013-09-24 18:46:39 +08:00   ❤️ 1
    一般的 Web 应用不会像这个示例这样占用 CPU,而是应该在等待 IO。

    如果想做这个测试可以用 gevent.sleep(1)。正式的应用要确保所有的第三方库(如 MySQL 客户端)都能够支持 gevent,才能在单进程单线程的 gevent 环境下不阻塞。
    ufo22940268
        5
    ufo22940268  
    OP
       2013-09-24 18:51:44 +08:00
    @lqs
    gunicorn -b 127.0.0.1:5000 -w 4 -k gevent --worker-connections=2000 a:app
    /Library/Python/2.7/site-packages/gunicorn/workers/ggevent.py:20: UserWarning: libevent version mismatch: system version is '2.0.21-stable' but this gevent is compiled against '2.0.19-stable'
    import gevent
    2013-09-24 18:49:34 [8930] [INFO] Starting gunicorn 18.0
    2013-09-24 18:49:34 [8930] [INFO] Listening at: http://127.0.0.1:5000 (8930)
    2013-09-24 18:49:34 [8930] [INFO] Using worker: gevent
    2013-09-24 18:49:34 [8933] [INFO] Booting worker with pid: 8933
    2013-09-24 18:49:34 [8934] [INFO] Booting worker with pid: 8934
    2013-09-24 18:49:34 [8935] [INFO] Booting worker with pid: 8935
    2013-09-24 18:49:34 [8936] [INFO] Booting worker with pid: 8936
    2013-09-24 18:49:37.355298
    2013-09-24 18:49:40.379017
    2013-09-24 18:49:43.393222
    2013-09-24 18:49:46.411903
    2013-09-24 18:49:49.426570

    我用了gevent.sleep(3), 结果还是拖拖的顺序执行,真不知道问题出在哪里。
    而且我已经配置了nginx了,不知道是不是那个warning的问题
    ufo22940268
        6
    ufo22940268  
    OP
       2013-09-24 18:58:10 +08:00
    重新编译了一次libevent, warning消除了,可是问题依然存在
    ufo22940268
        7
    ufo22940268  
    OP
       2013-09-24 21:41:19 +08:00
    这个问题我终于解决了。

    原来是我自己测试的方法不对,我在chrome中开了多个tab来测试,这样子就测不出来

    但是用一个chrome一个firefox来测试,或者ab -n 4 -c 4来测试就能够看出并发了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2234 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:07 · PVG 15:07 · LAX 00:07 · JFK 03:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.