Skip to content

Commit

Permalink
DummyClient of cache+memory:// backend now shares state between threa…
Browse files Browse the repository at this point in the history
…ds (#6524)
  • Loading branch information
matusvalo committed Dec 7, 2020
1 parent a192f9c commit c3e0410
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion celery/backends/cache.py
Expand Up @@ -20,6 +20,10 @@
Please use one of the following backends instead: {1}\
"""

# Global shared in-memory cache for in-memory cache client
# This is to share cache between threads
_DUMMY_CLIENT_CACHE = LRUCache(limit=5000)


def import_best_memcache():
if _imp[0] is None:
Expand Down Expand Up @@ -53,7 +57,7 @@ def Client(*args, **kwargs): # noqa
class DummyClient:

def __init__(self, *args, **kwargs):
self.cache = LRUCache(limit=5000)
self.cache = _DUMMY_CLIENT_CACHE

def get(self, key, *args, **kwargs):
return self.cache.get(key)
Expand Down
10 changes: 10 additions & 0 deletions t/unit/backends/test_cache.py
Expand Up @@ -35,6 +35,16 @@ def test_no_backend(self):
with pytest.raises(ImproperlyConfigured):
CacheBackend(backend=None, app=self.app)

def test_memory_client_is_shared(self):
"""This test verifies that memory:// backend state is shared over multiple threads"""
from threading import Thread
t = Thread(
target=lambda: CacheBackend(backend='memory://', app=self.app).set('test', 12345)
)
t.start()
t.join()
assert self.tb.client.get('test') == 12345

def test_mark_as_done(self):
assert self.tb.get_state(self.tid) == states.PENDING
assert self.tb.get_result(self.tid) is None
Expand Down

0 comments on commit c3e0410

Please sign in to comment.