使用多进程或多线程进行requests请求数据并比较score

要在Python中使用多进程或多线程并发地向多个接口请求数据,并且每个接口返回一个包含score字段的字典,可以使用requests库结合concurrent.futures模块来实现。这里将提供两种实现方式:一种使用多线程,另一种使用多进程。
1. 使用多线程
多线程适用于I/O密集型任务,在等待网络响应时可以让其他线程继续执行。
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
from pprint import pprint

# 定义请求的URL列表
urls = [
    'http://example.com/api1',
    'http://example.com/api2',
    'http://example.com/api3',
    # 更多URL...
]

def fetch_score(url):
    """ 发送请求并返回包含'score'字段的字典 """
    try:
        response = requests.get(url, timeout=10)
        response.raise_for_status()  # 如果请求失败,抛出异常
        data = response.json()
        if 'score' in data:
            return data
        else:
            return {'error': f'Missing score field in response from {url}'}
    except requests.exceptions.RequestException as e:
        return {'error': f'Request failed for {url}: {str(e)}'}

def main():
    # 使用线程池执行异步请求
    with ThreadPoolExecutor(max_workers=5) as executor:
        futures = {executor.submit(fetch_score, url): url for url in urls}
        
        # 收集所有的结果
        results = []
        for future in as_completed(futures):
            result = future.result()
            results.append(result)
        
        # 输出结果
        pprint(results)

if __name__ == '__main__':
    main()

2. 使用多进程
多进程适用于CPU密集型任务,但也可以用于I/O密集型任务。在Python中,多进程可以利用多个核心来并发执行任务。
import requests
from concurrent.futures import ProcessPoolExecutor, as_completed
from pprint import pprint

# 定义请求的URL列表
urls = [
    'http://example.com/api1',
    'http://example.com/api2',
    'http://example.com/api3',
    # 更多URL...
]

def fetch_score(url):
    """ 发送请求并返回包含'score'字段的字典 """
    try:
        response = requests.get(url, timeout=10)
        response.raise_for_status()  # 如果请求失败,抛出异常
        data = response.json()
        if 'score' in data:
            return data
        else:
            return {'error': f'Missing score field in response from {url}'}
    except requests.exceptions.RequestException as e:
        return {'error': f'Request failed for {url}: {str(e)}'}

def main():
    # 使用进程池执行异步请求
    with ProcessPoolExecutor(max_workers=5) as executor:
        futures = {executor.submit(fetch_score, url): url for url in urls}
        
        # 收集所有的结果
        results = []
        for future in as_completed(futures):
            result = future.result()
            results.append(result)
        
        # 输出结果
        pprint(results)

if __name__ == '__main__':
    main()

代码解释
1. 定义URL列表:urls列表包含了所有需要请求的API URL。
2. 定义请求函数:fetch_score函数用于向单个URL发送请求,并返回包含score字段的字典。
3. 主函数: 
•  使用ThreadPoolExecutor或ProcessPoolExecutor创建一个执行器。
•  提交多个任务给执行器,每个任务对应一个URL的请求。
•  使用as_completed函数来收集已完成的任务的结果。
•  将所有结果存储在一个列表中,并打印出来。
注意事项
•  异常处理:在fetch_score函数中加入了异常处理逻辑,确保在网络请求失败时能够捕获异常并返回错误信息。
•  并发数量:max_workers参数指定了线程池或进程池的最大并发数,默认为CPU核心数。可以根据实际情况调整这个参数。
•  JSON解析:假设API返回的是JSON格式的数据,如果返回的是其他格式,需要相应地调整解析逻辑。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李飞刀李寻欢

您的欣赏将是我奋斗路上的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值