推荐学习书目
› 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
ErgeAIA
V2EX  ›  Python

Python 多进程

  •  
  •   ErgeAIA ·
    ErgeAIA · Apr 19, 2018 · 4898 views
    This topic created in 3144 days ago, the information mentioned may be changed or developed.

    需求:批量检测 url 有效性

    我有一个文本中有 10w 条网址,需要检测所有 url 是否能打开,并将能打开的 url 保存到文本中,不了解 python 的多进程,网上看了一些文章,有些懵逼,求指点

    原代码如下:

    import requests
    
    url_result_success = []
    url_result_failed = []
    
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, compress',
        'Accept-Language': 'en-us;q=0.5,en;q=0.3',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0'
    }
    
    with open(r'urls.txt', 'r') as f:
        for url in f:
            url = url.strip()
            print url
            try:
                response = requests.get(url, headers=headers, allow_redirects=True, timeout=5)
                if response.status_code != 200:
                    raise requests.RequestException(u"Status code error: {}".format(response.status_code))
            except requests.RequestException as e:
                url_result_failed.append(url)
                continue
            url_result_success.append(url)
    
    with open(r'valid_urls.txt', 'a+') as f:
        for url in url_result_success:
            url = url.strip()
            f.write(url + '\n')
    
    23 replies  •  2018-04-20 13:23:57 +08:00
    zmj1316
    zmj1316
    Apr 19, 2018❤️1
    1
    IO 密集的不需要多进程,多线程甚至异步都可以
    skyleft
    skyleft
    Apr 19, 2018❤️1
    2
    gevent
    Nubia
    Nubia
    Apr 19, 2018❤️1
    3
    request 会阻塞 上 aiohttp 吧
    albertofwb
    albertofwb
    Apr 19, 2018 via Android❤️1
    4
    gevent 略重,async 了解下,python3.6+ 自带
    TheCure
    TheCure
    Apr 19, 2018❤️1
    5
    golang 了解下
    wzwwzw
    wzwwzw
    Apr 19, 2018❤️1
    6
    asyncio + aiohttp 了解下。
    doubleflower
    doubleflower
    Apr 19, 2018❤️1
    7
    开一百个线程就行,10w 没多久
    cszhiyue
    cszhiyue
    Apr 19, 2018❤️1
    8
    这并不是 cpu 密集的程序考虑 线程 或者 异步
    lusi1990
    lusi1990
    Apr 19, 2018 via Android❤️1
    9
    可以使用 head 代替 get,用多线程就可以,时间看带宽
    ToT
    ToT
    Apr 19, 2018❤️1
    10
    多线程即可,多进程会受限于你 CPU 的数目。
    ErgeAIA
    ErgeAIA
    OP
    Apr 19, 2018
    11
    @zmj1316 @Nubia @albertofwb @wzwwzw 代码要在 2003 上跑,没法装 3.6.。。
    @doubleflower @cszhiyue @ToT 嗯,用多线程
    @skyleft 找到过相关文章,值得研究
    @lusi1990 你说的知道,但没有深入看
    下午小区周边光纤让修地铁的干断了,才来的网。。。。
    ClutchBear
    ClutchBear
    Apr 19, 2018❤️1
    12
    生产者 消费者模式的多线程就行,
    用 queue 队列存取数据
    生产者往 queue 里面存数据,
    消费者里面用一个 while True 死循环 从 queue 里面取数据.
    设置一个终止的毒丸.
    ErgeAIA
    ErgeAIA
    OP
    Apr 19, 2018
    13
    @ClutchBear 要恶补基础知识了。。
    ClutchBear
    ClutchBear
    Apr 19, 2018❤️1
    14

    类似这样, 把读文件替换到 生产者里面,
    requests 验证放到消费者里面
    ErgeAIA
    ErgeAIA
    OP
    Apr 19, 2018
    15
    @ClutchBear 有推荐的书看么,程序设计模式之类的
    ClutchBear
    ClutchBear
    Apr 19, 2018
    16
    @Meli55a 多线程不需要书啊, 最基本的生产者 消费者模式,
    java python 都是一样的.
    ErgeAIA
    ErgeAIA
    OP
    Apr 19, 2018
    17
    @ClutchBear 好的,我好好看看你的代码,感谢!~
    claysec
    claysec
    Apr 19, 2018
    18
    @ClutchBear 我想了解下这两个 join 函数的意义是什么呢。萌新一时没看懂
    orangeade
    orangeade
    Apr 19, 2018 via Android❤️1
    19
    Python 里用多进程多线程直接上 concurrent future,基本上不用关心啥细节
    so1n
    so1n
    Apr 20, 2018 via Android
    20
    Aiohttp 了解下。。。
    L2AKnG8GXx60bc6P
    L2AKnG8GXx60bc6P
    Apr 20, 2018
    21
    grequests 了解下
    qiudays
    qiudays
    Apr 20, 2018
    22
    golang 了解下
    ox180
    ox180
    Apr 20, 2018
    23
    @orangeade 足够方便
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2104 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 70ms · UTC 04:23 · PVG 12:23 · LAX 20:23 · JFK 23:23
    ♥ Do have faith in what you're doing.