V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Apr 19, 2018 · 4466 views
    This topic created in 2942 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
        1
    zmj1316  
       Apr 19, 2018   ❤️ 1
    IO 密集的不需要多进程,多线程甚至异步都可以
    skyleft
        2
    skyleft  
       Apr 19, 2018   ❤️ 1
    gevent
    Nubia
        3
    Nubia  
       Apr 19, 2018   ❤️ 1
    request 会阻塞 上 aiohttp 吧
    albertofwb
        4
    albertofwb  
       Apr 19, 2018 via Android   ❤️ 1
    gevent 略重,async 了解下,python3.6+ 自带
    TheCure
        5
    TheCure  
       Apr 19, 2018   ❤️ 1
    golang 了解下
    wzwwzw
        6
    wzwwzw  
       Apr 19, 2018   ❤️ 1
    asyncio + aiohttp 了解下。
    doubleflower
        7
    doubleflower  
       Apr 19, 2018   ❤️ 1
    开一百个线程就行,10w 没多久
    cszhiyue
        8
    cszhiyue  
       Apr 19, 2018   ❤️ 1
    这并不是 cpu 密集的程序考虑 线程 或者 异步
    lusi1990
        9
    lusi1990  
       Apr 19, 2018 via Android   ❤️ 1
    可以使用 head 代替 get,用多线程就可以,时间看带宽
    ToT
        10
    ToT  
       Apr 19, 2018   ❤️ 1
    多线程即可,多进程会受限于你 CPU 的数目。
    Meli55a
        11
    Meli55a  
    OP
       Apr 19, 2018
    @zmj1316 @Nubia @albertofwb @wzwwzw 代码要在 2003 上跑,没法装 3.6.。。
    @doubleflower @cszhiyue @ToT 嗯,用多线程
    @skyleft 找到过相关文章,值得研究
    @lusi1990 你说的知道,但没有深入看
    下午小区周边光纤让修地铁的干断了,才来的网。。。。
    ClutchBear
        12
    ClutchBear  
       Apr 19, 2018   ❤️ 1
    生产者 消费者模式的多线程就行,
    用 queue 队列存取数据
    生产者往 queue 里面存数据,
    消费者里面用一个 while True 死循环 从 queue 里面取数据.
    设置一个终止的毒丸.
    Meli55a
        13
    Meli55a  
    OP
       Apr 19, 2018
    @ClutchBear 要恶补基础知识了。。
    ClutchBear
        14
    ClutchBear  
       Apr 19, 2018   ❤️ 1

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