can anyone help me with this?
When I call the heal_client method, and gc.collect() is executed, it successfully closes my database connection (just what I need). Is there something wrong with this approach, specifically with calling gc.collect() ?
(This code is part of a larger context involving asyncio and other libraries that got me to this point. It is expected that this method should almost never be called, but it has to be done because I have no other way to close the client and it is a long-running program.)
In conclusion, this approach seems to solve my problem, but I want to know if there might be a side effect that I am not seeing.
class RedisNotifier(INotifier):
def __init__(self):
self.__redis_client = redis.Redis(
host=settings.REDIS_PUBSUB_HOST,
port=settings.REDIS_PUBSUB_PORT,
password=settings.REDIS_PUBSUB_PASSWORD,
)
async def send_notification(self, room: str, notification: NotificationMessage) -> None:
await self.__redis_client.publish(room, notification.json())
def heal_client(self) -> None:
print("Healing redis client")
del self.__redis_client
gc.collect()
self.__redis_client = redis.Redis(
host=settings.REDIS_PUBSUB_HOST,
port=settings.REDIS_PUBSUB_PORT,
password=settings.REDIS_PUBSUB_PASSWORD,
)
print("done")