我发现 tornado 网络应用程序存在内存泄漏,但我不确定如何修复它。经过一些内存分析后,我发现我的 memcached 客户端在关闭客户端连接时泄漏了字典。作为 auto-discovery mechanism w/ AWS ElastiCache 的一部分,我经常打开/关闭 memcached 客户端(具体来说每分钟一次) .
这是一个使用 pympler 的最小复制器演示泄漏:
from pympler import muppy, summary
import asyncio
import aiomcache
loop = asyncio.get_event_loop()
async def hello_aiomcache():
mc = aiomcache.Client("127.0.0.1", 11211, loop=loop)
await mc.set(b"some_key", b"Some value")
value = await mc.get(b"some_key")
print(value)
values = await mc.multi_get(b"some_key", b"other_key")
print(values)
await mc.delete(b"another_key")
mc.close()
# establish a baseline (watch the <class 'dict line)
summary.print_(summary.summarize(muppy.get_objects()))
for i in range(50):
loop.run_until_complete(hello_aiomcache())
# <class 'dict grows
summary.print_(summary.summarize(muppy.get_objects()))
ds = [ao for ao in muppy.get_objects() if isinstance(ao, dict)]
# leaked dict looks like {'_loop': <_UnixSelectorEventLoop running=False closed=False debug=False>, '_paused': False, '_drain_waiter': None, '_connection_lost': False, '_stream_reader': <StreamReader t=<_SelectorSocketTransport fd=34 read=polling write=<idle, bufsize=0>>>, '_stream_writer': None, '_client_connected_cb': None, '_over_ssl': False}
ds[2364]
看起来这些字典将永远存在,直到 loop.close()
被调用。我对此感到困惑。我认为我不想关闭我通过 tornado.ioloop.IOLoop.IOLoop.current().asyncio_loop
从 tornado 借来的循环。有没有其他方法可以在不关闭循环的情况下正确关闭/清理这些连接?
最佳答案
问题是由于 await
ing mc.close()
引起的。
我有点惊讶地发现协程在没有明确调度的情况下实际上不会运行。我天真地认为它最终会在未来的某个时候被调用。然而,coroutine docs明确声明:
Calling a coroutine does not start its code running – the coroutine object returned by the call doesn’t do anything until you schedule its execution. There are two basic ways to start it running: call
await coroutine
oryield from coroutine
from another coroutine (assuming the other coroutine is already running!), or schedule its execution using the ensure_future() function or the AbstractEventLoop.create_task() method.
关于Python 3 asyncio 如何正确关闭客户端连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45024494/