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

Python 多进程

  •  
  •   Meli55a · 2018-04-19 17:17:34 +08:00 · 3746 次点击
    这是一个创建于 2188 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求:批量检测 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 条回复    2018-04-20 13:23:57 +08:00
    zmj1316
        1
    zmj1316  
       2018-04-19 18:18:59 +08:00   ❤️ 1
    IO 密集的不需要多进程,多线程甚至异步都可以
    skyleft
        2
    skyleft  
       2018-04-19 18:25:02 +08:00   ❤️ 1
    gevent
    Nubia
        3
    Nubia  
       2018-04-19 18:34:48 +08:00   ❤️ 1
    request 会阻塞 上 aiohttp 吧
    albertofwb
        4
    albertofwb  
       2018-04-19 18:35:20 +08:00 via Android   ❤️ 1
    gevent 略重,async 了解下,python3.6+ 自带
    TheCure
        5
    TheCure  
       2018-04-19 18:40:12 +08:00   ❤️ 1
    golang 了解下
    wzwwzw
        6
    wzwwzw  
       2018-04-19 18:50:02 +08:00   ❤️ 1
    asyncio + aiohttp 了解下。
    doubleflower
        7
    doubleflower  
       2018-04-19 18:57:49 +08:00   ❤️ 1
    开一百个线程就行,10w 没多久
    cszhiyue
        8
    cszhiyue  
       2018-04-19 19:06:45 +08:00   ❤️ 1
    这并不是 cpu 密集的程序考虑 线程 或者 异步
    lusi1990
        9
    lusi1990  
       2018-04-19 20:32:01 +08:00 via Android   ❤️ 1
    可以使用 head 代替 get,用多线程就可以,时间看带宽
    ToT
        10
    ToT  
       2018-04-19 22:19:15 +08:00   ❤️ 1
    多线程即可,多进程会受限于你 CPU 的数目。
    Meli55a
        11
    Meli55a  
    OP
       2018-04-19 22:50:04 +08:00
    @zmj1316 @Nubia @albertofwb @wzwwzw 代码要在 2003 上跑,没法装 3.6.。。
    @doubleflower @cszhiyue @ToT 嗯,用多线程
    @skyleft 找到过相关文章,值得研究
    @lusi1990 你说的知道,但没有深入看
    下午小区周边光纤让修地铁的干断了,才来的网。。。。
    ClutchBear
        12
    ClutchBear  
       2018-04-19 22:58:57 +08:00   ❤️ 1
    生产者 消费者模式的多线程就行,
    用 queue 队列存取数据
    生产者往 queue 里面存数据,
    消费者里面用一个 while True 死循环 从 queue 里面取数据.
    设置一个终止的毒丸.
    Meli55a
        13
    Meli55a  
    OP
       2018-04-19 23:07:08 +08:00
    @ClutchBear 要恶补基础知识了。。
    ClutchBear
        14
    ClutchBear  
       2018-04-19 23:07:37 +08:00   ❤️ 1

    类似这样, 把读文件替换到 生产者里面,
    requests 验证放到消费者里面
    Meli55a
        15
    Meli55a  
    OP
       2018-04-19 23:07:52 +08:00
    @ClutchBear 有推荐的书看么,程序设计模式之类的
    ClutchBear
        16
    ClutchBear  
       2018-04-19 23:09:33 +08:00
    @Meli55a 多线程不需要书啊, 最基本的生产者 消费者模式,
    java python 都是一样的.
    Meli55a
        17
    Meli55a  
    OP
       2018-04-19 23:16:57 +08:00
    @ClutchBear 好的,我好好看看你的代码,感谢!~
    claysec
        18
    claysec  
       2018-04-19 23:54:01 +08:00
    @ClutchBear 我想了解下这两个 join 函数的意义是什么呢。萌新一时没看懂
    orangeade
        19
    orangeade  
       2018-04-19 23:55:31 +08:00 via Android   ❤️ 1
    Python 里用多进程多线程直接上 concurrent future,基本上不用关心啥细节
    so1n
        20
    so1n  
       2018-04-20 00:23:39 +08:00 via Android
    Aiohttp 了解下。。。
    relic
        21
    relic  
       2018-04-20 10:22:41 +08:00
    grequests 了解下
    qiudays
        22
    qiudays  
       2018-04-20 10:36:01 +08:00
    golang 了解下
    ox180
        23
    ox180  
       2018-04-20 13:23:57 +08:00
    @orangeade 足够方便
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5638 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 110ms · UTC 06:00 · PVG 14:00 · LAX 23:00 · JFK 02:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.