我也来问个 gevent 的 threadpool 问题 - V2EX
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
hihihihihi

我也来问个 gevent 的 threadpool 问题

  •  
  •   hihihihihi 2016 年 9 月 22 日 3227 次点击
    这是一个创建于 3502 天前的主题,其中的信息可能已经有所发展或是发生改变。

    按我的理解, gevent 的 threadpool 应该就是通过管理 greenlet 的个数来控制当前的并发数吧。 按道理,使用 gevent 的 threadpool 不存在同步的问题呀。但是我在实际使用中,发现输出数据有点问题。

    代码如下:

    import gevent.monkey gevent.monkey.patch_socket() import gevent import requests from gevent.threadpool import ThreadPool from gevent.coros import Semaphore import datetime def fetch(pid): r = requests.get('http://www.baidu.com') print 'Process', pid, ':', len(r.text) def synchronous() for i in range(1,10): fetch(i) def asynchronous(): threads = [] for i in range(1,10): threads.append(gevent.spawn(fetch, i)) gevent.joinall(threads) def asynchronousPool(): pool = ThreadPool(10) for i in range(1,10): pool.spawn(fetch, i) gevent.wait() print('----------Synchronous----------') synchronous() print('----------Asynchronous----------') asynchronous() print('----------POOL----------') asynchronousPool() 

    程序输出的 output 如下:

    ----------Synchronous---------- Process 1 : 2381 Process 2 : 2381 Process 3 : 2381 Process 4 : 2381 Process 5 : 2381 Process 6 : 2381 Process 7 : 2381 Process 8 : 2381 Process 9 : 2381 ----------Asynchronous---------- Process 1 : 2381 Process 4 : 2381 Process 6 : 2381 Process 2 : 2381 Process 3 : 2381 Process 8 : 2381 Process 7 : 2381 Process 5 : 2381 Process 9 : 2381 ----------POOL---------- ProcessProcessProcess ProcessProcess Process25ProcessProcess 3 69 8::41 : :: :2381:2381: 2381 23812381 238123812381 Process 7 : 2381 

    很奇怪的是, 用 pool 的时候,每次输出的内容都是这样无须重叠的, 按我的理解,应该和中间的异步一样,虽然顺序随机,但是输出应该是一行一行整齐的呀?

    是不是我使用 gevent threadpool 打开方式不对?

    4 条回复    2016-09-23 01:21:10 +08:00
    hihihihihi
        1
    hihihihihi  
    OP
       2016 年 9 月 23 日
    我转发到 github 的开发组上面去问看看
    hihihihihi
        2
    hihihihihi  
    OP
       2016 年 9 月 23 日
    开发者真牛 x ,我发了消息,马上秒回。

    jamadden commented :

    A ThreadPool uses native threads, so you have exactly the same concerns that you'd have with any use of native threads.

    You probably don't want to use a ThreadPool here anyway, I doubt it's going to gain you any additional concurrency over what gevent already provides.
    hihihihihi
        3
    hihihihihi  
    OP
       2016 年 9 月 23 日
    可以关闭了。。。。

    可以关闭了。。。。

    可以关闭了。。。。
    hihihihihi
        4
    hihihihihi  
    OP
       2016 年 9 月 23 日 via iPhone
    用错了,应该用 pool 而不是 thread pool
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3720 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 00:16 PVG 08:16 LAX 17:16 JFK 20:16
    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