python3asyncio如何正确关闭客户端连接

2024-06-16 09:43:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个tornado web应用程序中发现了一个内存泄漏,我不知道如何修复它。经过一些内存分析后,我发现我的memcached客户端在关闭客户端连接时正在泄漏dict。作为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]

看起来这些dict将永远存在,直到调用loop.close()。我被这个弄糊涂了。我想我不想关闭我通过^{从tornado借来的循环。有没有其他方法可以在不关闭循环的情况下正确地关闭/清理这些连接?在


Tags: keyimportloopfalsegetobjectsvaluesome
1条回答
网友
1楼 · 发布于 2024-06-16 09:43:09

这个问题是由非awaiting 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 or yield 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.

相关问题 更多 >