Python asyncio/aiohttp: ValueError: too many file descriptors in select() on Windows

Python asyncio/aiohttp: ValueError: too many file descriptors in select() on Windows

注意:未来的读者请注意,这个问题是旧的,匆忙格式化和编程。给出的答案可能有用,但问题和代码可能没有用。

大家好,

我在理解 asyncio 和 aiohttp 以及让它们一起工作时遇到了问题。因为我不明白我在做什么,所以我 运行 遇到了一个我不知道如何解决的问题。

我正在使用 Windows 10 个 64 位。

下面的代码returns列出了Content-Typeheader中不包含“html”的页面。它是使用 asyncio 实现的。

import asyncio
import aiohttp

MAXitems = 30

async def getHeaders(url, session, sema):
    async with session:
        async with sema:
            try:
                async with session.head(url) as response:
                    try:
                        if "html" in response.headers["Content-Type"]:
                            return url, True
                        else:
                            return url, False
                    except:
                        return url, False
            except:
                return url, False


def check_urls_without_html(list_of_urls):
    headers_without_html = set()
    while(len(list_of_urls) != 0):
        blockurls = []
        print(len(list_of_urls))
        items = 0
        for num in range(0, len(list_of_urls)):
            if num < MAXitems:
                blockurls.append(list_of_urls[num - items])
                list_of_urls.remove(list_of_urls[num - items])
                items += 1
        loop = asyncio.get_event_loop()
        semaphoreHeaders = asyncio.Semaphore(50)
        session = aiohttp.ClientSession()
        data = loop.run_until_complete(asyncio.gather(*(getHeaders(url, session, semaphoreHeaders) for url in blockurls)))
        for header in data:
            if not header[1]:
                headers_without_html.add(header)
    return headers_without_html


list_of_urls= ['http://www.google.com', 'http://www.reddit.com']
headers_without_html =  check_urls_without_html(list_of_urls)

for header in headers_without_html:
    print(header[0])

当我 运行 它的 URL 太多(即 2000 个)时,有时会 returns 像这样的错误:

data = loop.run_until_complete(asyncio.gather(*(getHeaders(url, session, semaphoreHeaders) for url in blockurls)))
  File "USER\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 454, in run_until_complete
    self.run_forever()
  File "USER\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 421, in run_forever
    self._run_once()
  File "USER\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 1390, in _run_once
    event_list = self._selector.select(timeout)
  File "USER\AppData\Local\Programs\Python\Python36-32\lib\selectors.py", line 323, in select
    r, w, _ = self._select(self._readers, self._writers, [], timeout)
  File "USER\AppData\Local\Programs\Python\Python36-32\lib\selectors.py", line 314, in _select
    r, w, x = select.select(r, w, w, timeout)
ValueError: too many file descriptors in select()

我读到这个问题是由 Windows' 限制引起的。我也读过,除了尝试使用更少的文件描述符之外,对此无能为力。

我见过人们使用 asyncio 和 aiohttp 推送了数千个请求,但即使我很努力,我也无法推送 30-50 个请求而不会出现此错误。

我的代码是否存在根本性错误,或者这是 Windows 的固有问题?可以修复吗?可以增加 select 中允许的文件描述符的最大数量限制吗?

默认情况下 Windows 只能在异步循环中使用 64 个套接字。这是底层 select() API 调用的限制。

要增加限制请使用ProactorEventLoop,您可以使用下面的代码。在此处查看完整文档 here

if sys.platform == 'win32':
    loop = asyncio.ProactorEventLoop()
    asyncio.set_event_loop(loop)

另一种解决方案是使用信号量来限制整体并发,请参阅提供的答案 。例如,当执行 2000 API 次调用时,您可能不希望有太多并行打开请求(它们可能会超时/更难以查看单个调用时间)。这会给你

await gather_with_concurrency(100, *my_coroutines)

我遇到了同样的问题。不能 100% 确定这一定有效,但请尝试替换它:

session = aiohttp.ClientSession()

有了这个:

connector = aiohttp.TCPConnector(limit=60)
session = aiohttp.ClientSession(connector=connector)

默认情况下 limit 设置为 100 (docs),这意味着客户端可以同时打开 100 个连接。正如安德鲁提到的,Windows 一次只能打开 64 个套接字,因此我们提供一个低于 64 的数字。

#Add to call area
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)