python - 我在做什么错以及如何使用 python 的 asyncio 调试内存泄漏?
问题描述
我有这样的代码:
async def worker(self):
while True:
proxies = get_proxies()
for proxy in proxies:
await self.add_proxy_to_queue(proxy)
async def add_proxy_to_queue(self, proxy):
while self.proxies_semaphore.locked():
await asyncio.sleep(0.001)
asyncio.ensure_future(self.process_proxy(proxy))
async def process_proxy(self, proxy):
async with self.proxies_semaphore:
# send network request and wait for the resposne with timeout
我已经改变了它:
async def worker(self):
while True:
proxies = get_proxies()
for proxy in proxies:
await self.add_proxy_to_queue(proxy)
async def add_proxy_to_queue(self, proxy):
async with self.proxies_semaphore: # <-- HERE ARE THE CHANGES
asyncio.ensure_future(self.process_proxy(proxy))
async def process_proxy(self, proxy):
async with self.proxies_semaphore:
# send network request and wait for the resposne with timeout
它仍然在泄漏,但速度要慢得多。
我做错了什么以及如何调试异步代码,如何查看事件循环并查看实际存在哪些任务?
解决方案
推荐阅读
- css - 如何将 bootstrap-vue 图标包含到 nuxtjs 中?导航栏向下箭头的问题
- sequence - 在特定时间释放一个键会重置序列吗?
- linux - 升级 sonatype nexus 后权限被拒绝
- c++ - 如何解决 OMNest 5.5.1 中的此错误?
- r - R - 向量中特定值的嵌套循环
- javascript - 如何使用 vue-router 在 Vue js 中重定向到所需的页面?
- javascript - Node js - 批量 Odata
- php - 尝试使用 PHPMailer 时出现错误
- javascript - 在新的 vue js 项目中找不到模块“vuetify/lib”的声明文件
- nestjs - 为什么 NestJS GraphQL 验证错误没有放在错误消息字段中?