AttributeError:模块“队列”没有属性“ SimpleQueue”

时间:2019-11-25 09:29:14

标签: python-3.x asynchronous aiohttp

我接着输入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

1 个答案:

答案 0 :(得分:0)

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

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