我在做什么错,以及如何使用python的asyncio调试内存泄漏?

时间:2018-09-27 19:45:35

标签: python memory-leaks 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

,它仍在泄漏,但速度要慢得多。

我在做什么错了,如何调试异步代码,如何说看事件循环并查看实际上有哪些任务?

0 个答案:

没有答案