gpt4 book ai didi

python-3.x - 属性错误 : module 'queue' has no attribute 'SimpleQueue'

转载 作者:行者123 更新时间:2023-12-04 10:58:18 25 4
gpt4 key购买 nike

我跟着输入来自 https://realpython.com/async-io-python/#async-ios-roots-in-generators 的代码用 aiohttp 练习爬行。

尤其是当我开始对“”部分进行编码时完整程序:异步请求 ',我遇到了错误。

这是我的代码。

async def fetch_html(url: str, session: ClientSession, **kwargs) -> str:
resp = await session.request(method='GET', url=url, **kwargs)
resp.raise_for_status()
html = await resp.text()
return html

async def bulk_crawl_and_write(file: IO, urls: set, **kwargs) -> None:
urls = [
'https://regex101.com/'
]
async with ClientSession() as session:
tasks = []
for url in urls:
tasks.append(fetch_html(url=url, session=session, **kwargs))
await asyncio.gather(*tasks)

if __name__ == '__main__':
asyncio.run(bulk_crawl_and_write(file=None, urls=None))


和追溯。
Traceback (most recent call last):
File "areq2.py", line 108, in <module>
asyncio.run(bulk_crawl_and_write(file=outpath, urls=urls))
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
return future.result()
File "areq2.py", line 92, in bulk_crawl_and_write
await asyncio.gather(*tasks)
File "areq2.py", line 79, in write_one
async with aiofiles.open(file, "a") as f:
File "/Users/jun/Project/.venv_exp/lib/python3.7/site-packages/aiofiles/base.py", line 78, in __aenter__
self._obj = yield from self._coro
File "/Users/jun/Project/.venv_exp/lib/python3.7/site-packages/aiofiles/threadpool/__init__.py", line 35, in _open
f = yield from loop.run_in_executor(executor, cb)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 749, in run_in_executor
executor = concurrent.futures.ThreadPoolExecutor()
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 135, in __init__
self._work_queue = queue.SimpleQueue()
AttributeError: module 'queue' has no attribute 'SimpleQueue'


我从提供的链接中复制并粘贴了整个代码,以检查其余代码是否对正常运行至关重要,但它返回相同的错误。

我还检查了我的 python 版本,它是 3.7.3

最佳答案

SimpleQueue 是 Python 3.7 中的新功能 ( https://docs.python.org/3/library/queue.html#queue.SimpleQueue )

检查您的 Python 版本:python --version

关于python-3.x - 属性错误 : module 'queue' has no attribute 'SimpleQueue' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59028666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com