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

可以分享一下,当你遇到 python 多线程坑爹的时候的解决方案吗?

  •  
  •   VicYu ·
    Vic020 · Jan 13, 2016 · 3707 views
    This topic created in 3762 days ago, the information mentioned may be changed or developed.

    ok ,目前状况, Python2 ,都是 CPython 跑起的,也依赖了太多 CPython 包

    库中,同步版本一切 OK
    异步代码有 gevent 版本,但是许多三方包受不了 gevent 的 patch 纷纷跪掉,人力,时间又不够解决。结果就是在异步代码这方面就用 java 顶着, python 于是拥有了一个看着能用却用不了的版本。

    之前想过的方案:

    将众多 IO 放在一起开线程:异想天开了

    concurrent.futures :看见源码导入 threading 包,有点怕怕

    cython :但是没有找到哪里可以独立出计算的部分来用 c 改写

    multiprocessing : multiprocessing.queue 又跪了

    pypy : IO 问题

    等等

    Supplement 1  ·  Jan 13, 2016
    半瓶咣铛
    10 replies    2016-01-26 17:30:08 +08:00
    dai269619118
        1
    dai269619118  
       Jan 13, 2016
    python 多进程确实有点坑 用着用着程序就莫名其妙的挂掉
    后面只能去掉多线程,用异步来处理
    ethego
        2
    ethego  
       Jan 13, 2016
    你这怎么跪的又不详细说明一下,为什么怕 threading ?
    ipconfiger
        3
    ipconfiger  
       Jan 13, 2016
    @dai269619118 用着用着程序就莫名其妙的挂掉 多半是自己程序报错挂掉了吧
    VicYu
        4
    VicYu  
    OP
       Jan 13, 2016
    ethego
        5
    ethego  
       Jan 13, 2016
    @VicYu 这么担心 GIL , multiprocessing 怎么跪的?看着更像程序的问题,不要啥事都怪解释器。
    nooper
        6
    nooper  
       Jan 13, 2016
    程序写的有问题,却怪 python 问题。有很多解决方案的。
    ustslayer
        7
    ustslayer  
       Jan 13, 2016
    IO 为主("IO-bound")的程序不一定要用 multi-threading , gevent / twisted / tornado 这样的 event-based 的实现是更好的选择。 gevent 的 greenlet 容易死的话,用 twisted / tornado 这种非 stackless 的、更为安全的库我相信是很好的解决方案。

    CPU 为主("CPU-bound")的程序自然不应该用 CPython ,这种活当然是交给 C/C++ 甚至是 numba / OpenCL 来做了。
    mengzhuo
        8
    mengzhuo  
       Jan 14, 2016 via iPhone
    自己并发基础弱还怪 python ……
    你把代码拿出来让大家嘲笑和建议吧
    VicYu
        9
    VicYu  
    OP
       Jan 14, 2016
    @ustslayer 谢谢
    mikezhang0515
        10
    mikezhang0515  
       Jan 26, 2016
    个人理解,有 io 就搞多线程,简单踏实,用 Python 就别考虑极限性能
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2504 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 03:02 · PVG 11:02 · LAX 20:02 · JFK 23:02
    ♥ Do have faith in what you're doing.