Python 3 asyncio how to properly close client connection

I found a memory leak in a tornado web app and I'm not sure how to fix it. After some memory profiling, I found that my memcached client loses the dict when closing client connections. I am opening / closing memcached clients (once a minute to be specific) as part of the w / AWS ElastiCache auto-discovery mechanism .

Below is a minimal projector using pympler to demonstrate the leak:

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]

      

It looks like this dict will hang forever until it is called loop.close()

. I am confused by this. I think I don't want to close the loop that I borrowed from the tornado through tornado.ioloop.IOLoop.IOLoop.current().asyncio_loop

. Is there any other way to properly close / clear these connections without closing the loop?

+3


source to share


1 answer


The problem is not caused by await

ing mc.close()

.

I was a little surprised to learn that a coroutine won't work without some explicit scheduling. I naively thought that at some point in the future it would be called. However, the accompanying documents clearly state:



A coroutine call does not start from its code - a coroutine object returned by the call does nothing until you schedule it to execute. There are two main ways to run it: call await coroutine

or yield from coroutine

from another coroutine (assuming another coroutine is already running!), Or schedule it to run using the security_future () or AbstractEventLoop.create_task () method.

+1


source







All Articles