首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RuntimeWarning:启用tracemalloc,通过asyncio.sleep获取对象分配回溯

RuntimeWarning:启用tracemalloc,通过asyncio.sleep获取对象分配回溯
EN

Stack Overflow用户
提问于 2019-01-08 16:55:59
回答 1查看 86.4K关注 0票数 21

尝试使用信号量来控制异步请求来控制对目标主机的请求,但我得到了以下错误,我认为这意味着我的asycio.sleep()实际上并没有处于休眠状态。我该如何解决这个问题呢?我想为每个目标URL的请求添加延迟。

错误:

代码语言:javascript
复制
RuntimeWarning: coroutine 'sleep' was never awaited
Coroutine created at (most recent call last)
  File "sephora_scraper.py", line 71, in <module>
    loop.run_until_complete(main())
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 571, in run_until_complete
    self.run_forever()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 539, in run_forever
    self._run_once()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 1767, in _run_once
    handle._run()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "makeup.py", line 26, in get_html
    asyncio.sleep(delay)
  asyncio.sleep(delay)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

代码:

代码语言:javascript
复制
import sys
import time
import asyncio
import aiohttp

async def get_html(semaphore, session, url, delay=6):
    await semaphore.acquire()
    async with session.get(url) as res:
        html = await res.text()
        asyncio.sleep(delay)
        semaphore.release()
        return html

async def main():
    categories = {
        "makeup": "https://www.sephora.com/shop/"
    }
    semaphore = asyncio.Semaphore(value=1)
    tasks = []
    async with aiohttp.ClientSession(loop=loop, connector=aiohttp.TCPConnector(ssl=False)) as session:
        for category, url in categories.items():
                # Get HTML of all pages
            tasks.append(get_html(semaphore, session, url))
        res = await asyncio.gather(*tasks)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-08 18:41:50

代码语言:javascript
复制
asyncio.sleep(delay)

将其更改为:

代码语言:javascript
复制
await asyncio.sleep(delay)

asyncio.sleep是一个coroutine,应该等待。

票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54088263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档