Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can the use of threads + async + cachetools lead to an "event loop closed" error? #290

Closed
flixman opened this issue Oct 3, 2023 · 1 comment
Assignees
Labels

Comments

@flixman
Copy link

flixman commented Oct 3, 2023

This is not a bug but a question. I have an async Flask application that runs through gunicorn, with 1 worker and 4 threads. In one of the endpoints, which am caching with the asyncache cached decorator, I am doing an async call to another service. Many times I get back an "event loop closed" error. Is this to be expected? A sample of my code is:

from asyncache import cached
from cachetools import TTLCache

@cached(TTLCache(maxsize=32, ttl=60))
async def _retrieve_container_folders(account, aio_cred, container):
  return await call_to_async_method()

I have coded a thread-safe version of the TTLCache, like this

import threading

from cachetools import TTLCache as TTLCacheUpstream

_lock = threading.Lock()


class TTLCache(TTLCacheUpstream):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def __getitem__(self, *args, **kwargs):
        with _lock:
            return super().__getitem__(*args, **kwargs)

    def __setitem__(self, *args, **kwargs):
        with _lock:
            return super().__setitem__(*args, **kwargs)

But this, sometimes, just returns an empty result, so... I am pretty lost, here...

@flixman flixman added the bug label Oct 3, 2023
@tkem
Copy link
Owner

tkem commented Oct 24, 2023

This looks more like a question regarding asyncache, which is a separate project.

@tkem tkem closed this as completed Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants