关于asyncio的ValueError: too many file descriptors in select()错误

2024-08-27 05:38

本文主要是介绍关于asyncio的ValueError: too many file descriptors in select()错误,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近写爬虫用asyncio+aiohttp的形式,代码如下:

import aiohttp
import asyncioheaders = {"Upgrade-Insecure-Requests": "1","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Accept-Encoding": "gzip, deflate, sdch, br","Accept-Language": "zh-CN,zh;q=0.8",}async def ss(url):async with aiohttp.ClientSession() as session:async with session.get(url,headers=headers) as resp:print(resp.status)d = (await resp.text("utf-8","ignore"))cc(d)def cc(v):print(v)soup = BeautifulSoup(v, "lxml")contents = soup.select("div.content")for conten in contents:articleAuthor = conten.select("div.blog_info > a")if articleAuthor:# print(articleAuthor)articleAuthor = articleAuthor[0]else:articleAuthor = ""print(articleAuthor)loop = asyncio.get_event_loop()
tasks = [ss(url) for url in ["http://www.iteye.com/blogs/tag/java?page="+str(x) for x in range(1,2)] ]
loop.run_until_complete(asyncio.gather(*tasks))

乍一看代码没有问题,运行起来代码也没有问题,但是如果将url增加到上千个就会报ValueError: too many file descriptors in select()的错误

这是为什么呢?

因为asyncio内部用到了select,而select就是那个什么系统打开文件数是有限度的,上面的代码一次性将处理url的函数作为任务扔进了一个超大的List中,这就引起了错误,用这种形式无法写大规模爬虫

那怎么办呢?

用回调

代码如下:

from bs4 import BeautifulSoup
import aiohttp
import asyncio
import timeurlss=[]
headers = {"Upgrade-Insecure-Requests": "1","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Accept-Encoding": "gzip, deflate, sdch, br","Accept-Language": "zh-CN,zh;q=0.8",}async def ss(url):async with aiohttp.ClientSession() as session:async with session.get(url,headers=headers) as resp:print(resp.status)return await resp.text("utf-8","ignore")def cc(v):print("ssssssss")# print(v.result())# result()获取内容soup = BeautifulSoup(v.result(), "lxml")contents = soup.select("div.content")for conten in contents:# articleAuthor = conten.select("div.blog_info > a")# if articleAuthor:#     # print(articleAuthor)#     articleAuthor = articleAuthor[0]# else:#     articleAuthor = ""articleUrl = conten.select("h3 > a")if articleUrl:articleUrl = articleUrl[0].get("href")urlss.append(articleUrl)# async def ss2(url):
#     async with aiohttp.ClientSession() as session:
#         async with session.get(url,headers=headers) as resp:
#             print(resp.status)
#             return await resp.text("utf-8","ignore")def cc2(v):print("ssssssss222222222222")# print(v.result())# result()获取内容soup = BeautifulSoup(v.result(), "lxml")articleImages_list = soup.select("img")if articleImages_list:articleImages_list = articleImages_list[0].get("src")else:articleImages_list = []print(articleImages_list)now = lambda: time.time()
start = now()
loop = asyncio.get_event_loop()# url = "http://www.iteye.com/blogs/tag/java?page=1"
for url in ["http://www.iteye.com/blogs/tag/java?page="+str(x) for x in range(1,2)]:coroutine = ss(url)# 添加任务task = asyncio.ensure_future(coroutine)# 回调task.add_done_callback(cc)# 事件循环loop.run_until_complete(task)for url in urlss:coroutine = ss(url)task = asyncio.ensure_future(coroutine)task.add_done_callback(cc2)loop.run_until_complete(task)print('TIME: ', now() - start)



这篇关于关于asyncio的ValueError: too many file descriptors in select()错误的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110790

相关文章

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

SpringBoot3匹配Mybatis3的错误与解决方案

《SpringBoot3匹配Mybatis3的错误与解决方案》文章指出SpringBoot3与MyBatis3兼容性问题,因未更新MyBatis-Plus依赖至SpringBoot3专用坐标,导致类冲... 目录SpringBoot3匹配MyBATis3的错误与解决mybatis在SpringBoot3如果

Go中select多路复用的实现示例

《Go中select多路复用的实现示例》Go的select用于多通道通信,实现多路复用,支持随机选择、超时控制及非阻塞操作,建议合理使用以避免协程泄漏和死循环,感兴趣的可以了解一下... 目录一、什么是select基本语法:二、select 使用示例示例1:监听多个通道输入三、select的特性四、使用se

nginx配置错误日志的实现步骤

《nginx配置错误日志的实现步骤》配置nginx代理过程中,如果出现错误,需要看日志,可以把nginx日志配置出来,以便快速定位日志问题,下面就来介绍一下nginx配置错误日志的实现步骤,感兴趣的可... 目录前言nginx配置错误日志总结前言在配置nginx代理过程中,如果出现错误,需要看日志,可以把

Go语言使用select监听多个channel的示例详解

《Go语言使用select监听多个channel的示例详解》本文将聚焦Go并发中的一个强力工具,select,这篇文章将通过实际案例学习如何优雅地监听多个Channel,实现多任务处理、超时控制和非阻... 目录一、前言:为什么要使用select二、实战目标三、案例代码:监听两个任务结果和超时四、运行示例五

Python错误AttributeError: 'NoneType' object has no attribute问题的彻底解决方法

《Python错误AttributeError:NoneTypeobjecthasnoattribute问题的彻底解决方法》在Python项目开发和调试过程中,经常会碰到这样一个异常信息... 目录问题背景与概述错误解读:AttributeError: 'NoneType' object has no at

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.