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
4ever911
V2EX  ›  Python

我也来问个 gevent 的 threadpool 问题

  •  
  •   4ever911 · 2016-09-22 22:08:38 +08:00 · 2812 次点击
    这是一个创建于 2778 天前的主题,其中的信息可能已经有所发展或是发生改变。

    按我的理解, 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
    4ever911
        1
    4ever911  
    OP
       2016-09-23 00:18:33 +08:00
    我转发到 github 的开发组上面去问看看
    4ever911
        2
    4ever911  
    OP
       2016-09-23 00:50:25 +08:00
    开发者真牛 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.
    4ever911
        3
    4ever911  
    OP
       2016-09-23 00:52:22 +08:00
    可以关闭了。。。。

    可以关闭了。。。。

    可以关闭了。。。。
    4ever911
        4
    4ever911  
    OP
       2016-09-23 01:21:10 +08:00 via iPhone
    用错了,应该用 pool 而不是 thread pool
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2214 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:33 · PVG 10:33 · LAX 19:33 · JFK 22:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.