ResourceWarning: python-memcached not closing socket?

825 Views Asked by At

I'm running some unit tests with Python 3.6.1 and getting a ResourceWarning

ResourceWarning: unclosed <socket.socket fd=14, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 54897), raddr=('127.0.0.1', 11211)>

The port and area of code indicate it's memcached, and I'm using python-memcached 1.5.8. Relevant code is here:

if use_caching:
    import memcache
    mc = memcache.Client(['127.0.0.1:11211'], debug=0)

    key = 'descendent-catalog-ids-{0}'.format(str(cat_id))

    catalog_ids = mc.get(key)
    if catalog_ids is None:
        catalog_ids = get_descendent_ids(hierarchy_session)
        mc.set(key, catalog_ids)
else:
    catalog_ids = get_descendent_ids(hierarchy_session)

Am I supposed to manually close the memcached Client instance somehow? I can't find any reference in the source code or docs about manually closing the socket, so I assumed that the library would handle that automagically...

I haven't found any similar questions (only one for urllib), and there aren't any relevant issues in GitHub, so my assumption is that I'm missing something simple.

1

There are 1 best solutions below

0
On

yes, there is a close method for pymemcached client: https://pymemcache.readthedocs.io/en/latest/apidoc/pymemcache.client.base.html#pymemcache.client.base.Client.close

Or you can write your own manager:

class Cache(object):
"""
General caching wrapper for accessing shared objects across machines or processes
"""

def __init__(self):
    self.client = None
    self._host = os.environ["MEMCACHED_HOST"]
    self._port = 11211

def __enter__(self):
    self.client = base.Client((self._host, self._port))
    return self.client

def __exit__(self, type, value, traceback):
    self.client.close()