简体   繁体   English

RuntimeWarning:启用 tracemalloc 以使用 asyncio.sleep 获取对象分配回溯

[英]RuntimeWarning: Enable tracemalloc to get the object allocation traceback with asyncio.sleep

Trying to use a semaphore to control asynchronous requests to control the requests to my target host but I am getting the following error which I have assume means that my asycio.sleep() is not actually sleeping.尝试使用信号量来控制异步请求以控制对目标主机的请求,但我收到以下错误,我认为这意味着我的asycio.sleep()实际上并未休眠。 How can I fix this?我怎样才能解决这个问题? I want to add a delay to my requests for each URL targeted.我想为每个目标 URL 的请求添加延迟。

Error:错误:

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

Code:代码:

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())
asyncio.sleep(delay)

Change it to:将其更改为:

await asyncio.sleep(delay)

asyncio.sleep is a coroutine and should be awaited. asyncio.sleep是一个 协程,应该等待。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RuntimeWarning:启用tracemalloc以获取对象分配回溯 - RuntimeWarning: Enable tracemalloc to get the object allocation traceback 为什么我会收到 RuntimeWarning: Enable tracemalloc to get the object allocation traceback from asyncio.run? - Why am i getting RuntimeWarning: Enable tracemalloc to get the object allocation traceback from asyncio.run? 如何修复 RuntimeWarning:启用 tracemalloc 以获取 object 分配回溯错误 - How to fix RuntimeWarning: Enable tracemalloc to get the object allocation traceback error discord.py, RuntimeWarning: 启用 tracemalloc 以获取发送消息时的对象分配回溯 - discord.py, RuntimeWarning: Enable tracemalloc to get the object allocation traceback on sending message 启用 tracemalloc 以获取 object 分配回溯 PYTHON - Enable tracemalloc to get the object allocation traceback PYTHON 使用tornado.httpclient.AsyncHTTPClient时,如何修复“RuntimeWarning:启用tracemalloc以获取对象分配回溯”? - How to fix “RuntimeWarning: Enable tracemalloc to get the object allocation traceback” when using tornado.httpclient.AsyncHTTPClient? “RuntimeWarning: Enable tracemalloc to get the object allocation traceback”是什么意思? 我知道如何修复,寻找意义 - What does "RuntimeWarning: Enable tracemalloc to get the object allocation traceback" mean? I know how to fix, looking for the meaning Python Selenium - ResourceWarning:启用 tracemalloc 以获取对象分配回溯 - Python Selenium - ResourceWarning: Enable tracemalloc to get the object allocation traceback Python asyncio.sleep - Python asyncio.sleep await Asyncio.sleep 是否不一致? - Is await Asyncio.sleep inconsistent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM