-
Notifications
You must be signed in to change notification settings - Fork 2.6k
is it caused by the unsafe threads? #2865
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
Comments
When you close a connection, it gets returned to the pool. |
@evgenybf thanks mate to take a look. I am not sure if i am fully understand you so please correct me if i am wrong. Do you mean we should try to create the connection pool for each single test function? And thats able to make sure the connection_pool and event loops create and close together? |
Yes
It will make sure that connection opened in one event loop won't be reused in another one. Check out async def aclose(self, close_connection_pool: Optional[bool] = None) -> None:
"""
Closes Redis client connection
:param close_connection_pool: decides whether to close the connection pool used
by this Redis client, overriding Redis.auto_close_connection_pool. By default,
let Redis.auto_close_connection_pool decide whether to close the connection
pool.
"""
conn = self.connection
if conn:
self.connection = None
await self.connection_pool.release(conn) # <---- this is not an actual close. It releases the connection so it can be reused on the next get_connection() call
if close_connection_pool or (
close_connection_pool is None and self.auto_close_connection_pool
):
await self.connection_pool.disconnect() This def get_connection_async_redis():
"""get a new connection instance for redis (for async framework)"""
pool = aioredis.ConnectionPool(
host='localhost',
port=6379,
db=0,
# connection_class=aioredis.SSLConnection,
# ssl_cert_reqs=None,
)
try:
return aioredis.Redis(
connection_pool = pool,
auto_close_connection_pool=False
)
except IOError:
logger.exception("Error when connecting to redis...") Alternatively, you can close the pool in Since I don't usually have more than one event loop running, I prefer having a global |
Really appreciate the details! @evgenybf you are the legend!
|
Sorry, I tested it with redis-py 5.0.1 only. With redis-py 4.5.3 calling class ConnectionPool(...):
...
def reset(self):
self._lock = asyncio.Lock()
self._created_connections = 0
self._available_connections = []
self._in_use_connections = set()
# this must be the last operation in this method. while reset() is
# called when holding _fork_lock, other threads in this process
# can call _checkpid() which compares self.pid and os.getpid() without
# holding any lock (for performance reasons). keeping this assignment
# as the last operation ensures that those other threads will also
# notice a pid difference and block waiting for the first thread to
# release _fork_lock. when each of these threads eventually acquire
# _fork_lock, they will notice that another thread already called
# reset() and they will immediately release _fork_lock and continue on.
self.pid = os.getpid() class TestPublicHolidaysDataRetrieval(IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
await clear_cache("key")
async def asyncTearDown(self) -> None:
await pool.disconnect()
pool.reset()
async def test_func_1(self) -> None:
...
async def test_func_2(self) -> None:
... With redis-py==4.5.3
With redis-py==5.0.1
|
Thanks a lot! @evgenybf
|
@JerryHuang-LE same as yours. Would you mind sharing the traceback? Since, |
See the test. It's supposed to be run in Docker |
@evgenybf You are the legend. It is working for me currently. Thanks mate. |
Closing this issue as it has already been answered. |
Version:
Platform:
Description:
redis.py
cahce.py:
unit_test.py:
When you run the unit test (please make sure you have multiple functions need to be tested), it will throw out the error:
The text was updated successfully, but these errors were encountered: