Python进程池和线程池

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python进程池和线程池相关的知识,希望对你有一定的参考价值。

代码

在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢。

import requests
from concurrent.futures import ThreadPoolExecutor  #引入线程池模块

def asyns_url(url):
    try:
        response = requests.get(url)
    except Exception as e:
        print(异常结果,response.url,response.content)
    print(获取结果, response.url, response.content)
url_list ={
    http://www.baidu.com,
    http://www/google.com,
    http://dig.chouti.com,
    http://www.bing.com
}
pool =ThreadPoolExecutor(5)
for url in  url_list:
    print(开始请求,url)
    pool.submit(asyns_url,url)
pool.shutdown(wait=True)#终止线程

 

技术分享
from concurrent.futures import ProcessPoolExecutor
import requests

def fetch_async(url):
    response = requests.get(url)
    return response


url_list = [http://www.github.com, http://www.bing.com]
pool = ProcessPoolExecutor(5)
for url in url_list:
    pool.submit(fetch_async, url)
pool.shutdown(wait=True)

3.多进程执行
多进程
技术分享
from concurrent.futures import ProcessPoolExecutor
import requests


def fetch_async(url):
    response = requests.get(url)
    return response


def callback(future):
    print(future.result())


url_list = [http://www.github.com, http://www.bing.com]
pool = ProcessPoolExecutor(5)
for url in url_list:
    v = pool.submit(fetch_async, url)
    v.add_done_callback(callback)
pool.shutdown(wait=True)

3.多进程+回调函数执行
多进程+回调函数

 

以上是关于Python进程池和线程池的主要内容,如果未能解决你的问题,请参考以下文章

2020Python修炼记python并发编程补充—进程池和线程池

Python中的进程池和线程池

Python并发复习4- concurrent.futures模块(线程池和进程池)

python爬虫之线程池和进程池

python线程池和进程池的多参数调用

开启线程池和进程池