Skip to content

Commit a0384e5

Browse files
committed
Fixed event loop bug with synchronous call
1 parent 4b0af8c commit a0384e5

File tree

10 files changed

+128
-47
lines changed

10 files changed

+128
-47
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[flake8]
22
max-line-length = 88
3-
ignore = E203, E241, E501, W503, F811
3+
ignore = E203, E241, E501, W503, F811, W601
44
exclude =
55
.git,
66
__pycache__

ellar/cache/backends/aio_cache.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import asyncio
21
import pickle
32
import typing as t
43
from abc import ABC
54

5+
from ellar.helper.event_loop import get_or_create_eventloop
6+
67
try:
78
from aiomcache import Client
89
except ImportError as e: # pragma: no cover
@@ -18,7 +19,7 @@
1819

1920
class AioMemCacheBackendSync(IBaseCacheBackendAsync, ABC):
2021
def _async_executor(self, func: t.Awaitable) -> t.Any:
21-
return asyncio.get_event_loop().run_until_complete(func)
22+
return get_or_create_eventloop().run_until_complete(func)
2223

2324
def get(self, key: str, version: str = None) -> t.Any:
2425
return self._async_executor(self.get_async(key, version=version))
@@ -28,14 +29,20 @@ def delete(self, key: str, version: str = None) -> bool:
2829
return bool(res)
2930

3031
def set(
31-
self, key: str, value: t.Any, timeout: int = None, version: str = None
32+
self,
33+
key: str,
34+
value: t.Any,
35+
timeout: t.Union[float, int] = None,
36+
version: str = None,
3237
) -> bool:
3338
res = self._async_executor(
3439
self.set_async(key, value, version=version, timeout=timeout)
3540
)
3641
return bool(res)
3742

38-
def touch(self, key: str, timeout: int = None, version: str = None) -> bool:
43+
def touch(
44+
self, key: str, timeout: t.Union[float, int] = None, version: str = None
45+
) -> bool:
3946
res = self._async_executor(
4047
self.touch_async(key, version=version, timeout=timeout)
4148
)
@@ -65,7 +72,7 @@ def __init__(
6572
self._serializer = serializer
6673
self._deserializer = deserializer
6774

68-
def get_backend_timeout(self, timeout: int = None) -> int:
75+
def get_backend_timeout(self, timeout: t.Union[float, int] = None) -> int:
6976
return int(super().get_backend_timeout(timeout))
7077

7178
@property
@@ -83,7 +90,11 @@ async def get_async(self, key: str, version: str = None) -> t.Optional[t.Any]:
8390

8491
@make_key_decorator
8592
async def set_async(
86-
self, key: str, value: t.Any, timeout: int = None, version: str = None
93+
self,
94+
key: str,
95+
value: t.Any,
96+
timeout: t.Union[float, int] = None,
97+
version: str = None,
8798
) -> bool:
8899
return await self._cache_client.set(
89100
key.encode("utf-8"),
@@ -97,7 +108,7 @@ async def delete_async(self, key: str, version: str = None) -> bool:
97108

98109
@make_key_decorator
99110
async def touch_async(
100-
self, key: str, timeout: int = None, version: str = None
111+
self, key: str, timeout: t.Union[float, int] = None, version: str = None
101112
) -> bool:
102113
return await self._cache_client.touch(
103114
key=key.encode("utf-8"), exptime=self.get_backend_timeout(timeout)

ellar/cache/backends/base.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ class BasePylibMemcachedCacheSync(BaseCacheBackend, ABC):
1111
_cache_client: t.Any
1212

1313
@make_key_decorator
14-
async def get(self, key: str, version: str = None) -> t.Any:
14+
def get(self, key: str, version: str = None) -> t.Any:
1515
return self._cache_client.get(key)
1616

1717
@make_key_decorator_and_validate
1818
def set(
19-
self, key: str, value: t.Any, timeout: int = None, version: str = None
19+
self,
20+
key: str,
21+
value: t.Any,
22+
timeout: t.Union[float, int] = None,
23+
version: str = None,
2024
) -> bool:
2125
result = self._cache_client.set(
2226
key, value, int(self.get_backend_timeout(timeout))
@@ -34,7 +38,9 @@ def delete(self, key: str, version: str = None) -> bool:
3438
return bool(result)
3539

3640
@make_key_decorator
37-
def touch(self, key: str, timeout: int = None, version: str = None) -> bool:
41+
def touch(
42+
self, key: str, timeout: t.Union[float, int] = None, version: str = None
43+
) -> bool:
3844
result = self._cache_client.touch(key, self.get_backend_timeout(timeout))
3945
return bool(result)
4046

@@ -83,7 +89,11 @@ async def get_async(self, key: str, version: str = None) -> t.Any:
8389
return await self.executor(self.get, key)
8490

8591
async def set_async(
86-
self, key: str, value: t.Any, timeout: int = None, version: str = None
92+
self,
93+
key: str,
94+
value: t.Any,
95+
timeout: t.Union[float, int] = None,
96+
version: str = None,
8797
) -> bool:
8898
result = await self.executor(self.set, key, value, timeout)
8999
return bool(result)
@@ -93,7 +103,7 @@ async def delete_async(self, key: str, version: str = None) -> bool:
93103
return bool(result)
94104

95105
async def touch_async(
96-
self, key: str, timeout: int = None, version: str = None
106+
self, key: str, timeout: t.Union[float, int] = None, version: str = None
97107
) -> bool:
98108
result = await self.executor(self.touch, key, timeout)
99109
return bool(result)

ellar/cache/backends/redis_cache.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ def delete(self, key: str, version: str = None) -> bool:
2727
return bool(res)
2828

2929
def set(
30-
self, key: str, value: t.Any, timeout: int = None, version: str = None
30+
self,
31+
key: str,
32+
value: t.Any,
33+
timeout: t.Union[float, int] = None,
34+
version: str = None,
3135
) -> bool:
3236
res = self._async_executor(
3337
self.set_async(key, value, version=version, timeout=timeout)
3438
)
3539
return bool(res)
3640

37-
def touch(self, key: str, timeout: int = None, version: str = None) -> bool:
41+
def touch(
42+
self, key: str, timeout: t.Union[float, int] = None, version: str = None
43+
) -> bool:
3844
res = self._async_executor(
3945
self.touch_async(key, version=version, timeout=timeout)
4046
)
@@ -73,7 +79,9 @@ def __init__(
7379
self._serializer = serializer
7480
self._deserializer = deserializer
7581

76-
def get_backend_timeout(self, timeout: int = None) -> t.Union[float, int]:
82+
def get_backend_timeout(
83+
self, timeout: t.Union[float, int] = None
84+
) -> t.Union[float, int]:
7785
if timeout is None:
7886
timeout = self._default_timeout
7987
# The key will be made persistent if None used as a timeout.
@@ -99,7 +107,11 @@ async def get_async(self, key: str, version: str = None) -> t.Any:
99107

100108
@make_key_decorator
101109
async def set_async(
102-
self, key: str, value: t.Any, timeout: int = None, version: str = None
110+
self,
111+
key: str,
112+
value: t.Any,
113+
timeout: t.Union[float, int] = None,
114+
version: str = None,
103115
) -> bool:
104116
value = self._serializer(value, self.pickle_protocol)
105117
if timeout == 0:
@@ -118,7 +130,7 @@ async def delete_async(self, key: str, version: str = None) -> bool:
118130

119131
@make_key_decorator
120132
async def touch_async(
121-
self, key: str, timeout: int = None, version: str = None
133+
self, key: str, timeout: t.Union[float, int] = None, version: str = None
122134
) -> bool:
123135
if timeout is None:
124136
res = await self._cache_client.persist(key)

ellar/cache/backends/simple_cache.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
import pickle
32
import time
43
import typing as t
@@ -7,14 +6,16 @@
76

87
from anyio import Lock
98

9+
from ellar.helper.event_loop import get_or_create_eventloop
10+
1011
from ..interface import IBaseCacheBackendAsync
1112
from ..make_key_decorator import make_key_decorator, make_key_decorator_and_validate
1213
from ..model import BaseCacheBackend
1314

1415

1516
class SimpleCacheBackendSync(IBaseCacheBackendAsync, ABC):
1617
def _async_executor(self, func: t.Awaitable) -> t.Any:
17-
return asyncio.get_event_loop().run_until_complete(func)
18+
return get_or_create_eventloop().run_until_complete(func)
1819

1920
def get(self, key: str, version: str = None) -> t.Any:
2021
return self._async_executor(self.get_async(key, version=version))
@@ -24,14 +25,20 @@ def delete(self, key: str, version: str = None) -> bool:
2425
return bool(res)
2526

2627
def set(
27-
self, key: str, value: t.Any, timeout: int = None, version: str = None
28+
self,
29+
key: str,
30+
value: t.Any,
31+
timeout: t.Union[float, int] = None,
32+
version: str = None,
2833
) -> bool:
2934
res = self._async_executor(
3035
self.set_async(key, value, timeout=timeout, version=version)
3136
)
3237
return bool(res)
3338

34-
def touch(self, key: str, timeout: int = None, version: str = None) -> bool:
39+
def touch(
40+
self, key: str, timeout: t.Union[float, int] = None, version: str = None
41+
) -> bool:
3542
res = self._async_executor(
3643
self.touch_async(key, timeout=timeout, version=version)
3744
)
@@ -72,7 +79,11 @@ async def delete_async(self, key: str, version: str = None) -> bool:
7279

7380
@make_key_decorator_and_validate
7481
async def set_async(
75-
self, key: str, value: t.Any, timeout: int = None, version: str = None
82+
self,
83+
key: str,
84+
value: t.Any,
85+
timeout: t.Union[float, int] = None,
86+
version: str = None,
7687
) -> bool:
7788
async with self._lock:
7889
self._cache[key] = pickle.dumps(value, self.pickle_protocol)
@@ -93,7 +104,7 @@ async def has_key_async(self, key: str, version: str = None) -> bool:
93104

94105
@make_key_decorator
95106
async def touch_async(
96-
self, key: str, timeout: int = None, version: str = None
107+
self, key: str, timeout: t.Union[float, int] = None, version: str = None
97108
) -> bool:
98109
async with self._lock:
99110
if self._has_expired(key):

ellar/cache/interface.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ async def delete_async(self, key: str, version: str = None) -> bool:
2222

2323
@abstractmethod
2424
async def set_async(
25-
self, key: str, value: t.Any, timeout: int = None, version: str = None
25+
self,
26+
key: str,
27+
value: t.Any,
28+
timeout: t.Union[float, int] = None,
29+
version: str = None,
2630
) -> bool:
2731
"""Add a new key/value to the cache (overwrites value, if key already
2832
exists in the cache).
@@ -40,7 +44,7 @@ async def set_async(
4044

4145
@abstractmethod
4246
async def touch_async(
43-
self, key: str, timeout: int = None, version: str = None
47+
self, key: str, timeout: t.Union[float, int] = None, version: str = None
4448
) -> bool:
4549
"""
4650
Update the key's expiry time using timeout. Return True if successful
@@ -68,7 +72,11 @@ def delete(self, key: str, version: str = None) -> bool:
6872

6973
@abstractmethod
7074
def set(
71-
self, key: str, value: t.Any, timeout: int = None, version: str = None
75+
self,
76+
key: str,
77+
value: t.Any,
78+
timeout: t.Union[float, int] = None,
79+
version: str = None,
7280
) -> bool:
7381
"""Add a new key/value to the cache (overwrites value, if key already
7482
exists in the cache).
@@ -85,7 +93,9 @@ def set(
8593
"""
8694

8795
@abstractmethod
88-
def touch(self, key: str, timeout: int = None, version: str = None) -> bool:
96+
def touch(
97+
self, key: str, timeout: t.Union[float, int] = None, version: str = None
98+
) -> bool:
8999
"""
90100
Update the key's expiry time using timeout. Return True if successful
91101
or False if the key does not exist.
@@ -117,7 +127,7 @@ def set(
117127
self,
118128
key: str,
119129
value: t.Any,
120-
timeout: int = None,
130+
timeout: t.Union[float, int] = None,
121131
version: str = None,
122132
backend: str = None,
123133
) -> bool:
@@ -138,7 +148,11 @@ def set(
138148

139149
@abstractmethod
140150
def touch(
141-
self, key: str, timeout: int = None, version: str = None, backend: str = None
151+
self,
152+
key: str,
153+
timeout: t.Union[float, int] = None,
154+
version: str = None,
155+
backend: str = None,
142156
) -> bool:
143157
"""
144158
Update the key's expiry time using timeout. Return True if successful
@@ -181,7 +195,7 @@ async def set_async(
181195
self,
182196
key: str,
183197
value: t.Any,
184-
timeout: int = None,
198+
timeout: t.Union[float, int] = None,
185199
version: str = None,
186200
backend: str = None,
187201
) -> bool:
@@ -202,7 +216,11 @@ async def set_async(
202216

203217
@abstractmethod
204218
async def touch_async(
205-
self, key: str, timeout: int = None, version: str = None, backend: str = None
219+
self,
220+
key: str,
221+
timeout: t.Union[float, int] = None,
222+
version: str = None,
223+
backend: str = None,
206224
) -> bool:
207225
"""
208226
Update the key's expiry time using timeout. Return True if successful

ellar/cache/model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ def make_key(self, key: str, version: str = None) -> str:
6161
"""
6262
return "%s:%s:%s" % (self._key_prefix, version or self._version, key)
6363

64-
def get_backend_timeout(self, timeout: int = None) -> t.Union[float, int]:
64+
def get_backend_timeout(
65+
self, timeout: t.Union[float, int] = None
66+
) -> t.Union[float, int]:
6567
"""
6668
Return the timeout value usable by this backend based upon the provided
6769
timeout.

0 commit comments

Comments
 (0)