V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
nullllun
V2EX  ›  问与答

新手一枚,用 python gevent 抓取网页碰到了一些问题,求解答

  •  
  •   nullllun · 2015-04-20 23:31:51 +08:00 · 1377 次点击
    这是一个创建于 3357 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码放在网盘里了,其中fetcher.py是主要模块:
    http://pan.baidu.com/s/1pJMFpxD

    fetcher.py中distribute函数用于派发任务,也就是往协程池里添加协程,代码如下:

    for url in task.urls:
    proxy = self.getProxy()
    print '[pid %d time %f] conPool size: %d distributing url: %s' %(os.getpid(), time.time(), len(self.pool), url)
    self.pool.apply_async(self.fetch, args=(url, 'static', proxy))

    理论上for循环能很快派发完任务,但是结果却不是这样的,1秒钟内只派发了几个任务:
    [pid 5896 time 1429543816.844000] conPool size: 14 distributing url: http://hao123.com/
    [pid 5896 time 1429543816.940000] conPool size: 15 distributing url: http://zol.com.cn/
    [pid 5896 time 1429543817.035000] conPool size: 16 distributing url: http://tudou.com/
    [pid 5896 time 1429543817.144000] conPool size: 17 distributing url: http://cntv.cn/
    [pid 5896 time 1429543817.239000] conPool size: 18 distributing url: http://china.com.cn/
    [pid 5896 time 1429543817.298000] conPool size: 19 distributing url: http://southcn.com/
    [pid 5896 time 1429543817.304000] conPool size: 20 distributing url: http://taobao.com/
    [pid 5896 time 1429543817.308000] conPool size: 21 distributing url: http://douban.com/
    [pid 5896 time 1429543817.311000] conPool size: 22 distributing url: http://58.com/
    [pid 5896 time 1429543817.315000] conPool size: 23 distributing url: http://tianya.cn/
    [pid 5896 time 1429543817.361000] conPool size: 24 distributing url: http://ganji.com/
    [pid 5896 time 1429543817.487000] conPool size: 25 distributing url: http://pconline.com.cn/
    [pid 5896 time 1429543817.592000] conPool size: 26 distributing url: http://eastmoney.com/
    [pid 5896 time 1429543817.646000] conPool size: 27 distributing url: http://bitauto.com/
    [pid 5896 time 1429543818.759000] conPool size: 28 distributing url: http://autohome.com.cn/
    [pid 5896 time 1429543818.778000] conPool size: 29 distributing url: http://39.net/
    [pid 5896 time 1429543818.858000] conPool size: 30 distributing url: http://iqiyi.com/
    [pid 5896 time 1429543818.949000] conPool size: 31 distributing url: http://ctrip.com/

    用wing ide调试了一下,发现代码有时碰到print语句都会切换协程,很不理解。本人新手一个,代码写的很渣,求轻拍。
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2467 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:24 · PVG 21:24 · LAX 06:24 · JFK 09:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.