Python RuntimeWarning:启用tracemalloc以使用asyncio.sleep获取对象分配回溯
试图使用信号量控制异步请求以控制对目标主机的请求,但我得到了以下错误,我假设这意味着我的Python RuntimeWarning:启用tracemalloc以使用asyncio.sleep获取对象分配回溯,python,semaphore,python-asyncio,aiohttp,Python,Semaphore,Python Asyncio,Aiohttp,试图使用信号量控制异步请求以控制对目标主机的请求,但我得到了以下错误,我假设这意味着我的asycio.sleep()实际上没有睡眠。我怎样才能解决这个问题?我想为每个目标URL的请求添加延迟 错误: RuntimeWarning: coroutine 'sleep' was never awaited Coroutine created at (most recent call last) File "sephora_scraper.py", line 71, in <module&g
asycio.sleep()
实际上没有睡眠。我怎样才能解决这个问题?我想为每个目标URL的请求添加延迟
错误:
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
将其更改为:
await asyncio.sleep(delay)
asyncio.sleep
是一个问题,应该等待解决。也可以使用async with
或至少try/except
与信号量一起使用,如图所示。它将保证即使在异常情况下也会释放信号量。
asyncio.sleep(delay)
await asyncio.sleep(delay)