Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
fix(async): move aiohttp session from class attribute to function scope
Browse files Browse the repository at this point in the history
  • Loading branch information
NTGNguyen committed Dec 31, 2024
1 parent d8820b0 commit b9a7ba4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/check_phat_nguoi/notify/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def __init__(
):
self._telegram_notify_object: TelegramNotifyDTO = telegram_notify
self._message_dict: dict[str, LiteralString] = message_dict
self.session = ClientSession()
self.timeout = 10

async def _send_message(self, message: LiteralString) -> None:
Expand All @@ -32,8 +31,9 @@ async def _send_message(self, message: LiteralString) -> None:
"text": message,
"parse_mode": "Markdown",
}
session = ClientSession()
try:
async with self.session.post(
async with session.post(
url, json=payload, timeout=ClientTimeout(self.timeout)
) as response:
response.raise_for_status()
Expand All @@ -57,6 +57,8 @@ async def _send_message(self, message: LiteralString) -> None:
bot_token=self._telegram_notify_object.telegram.bot_token,
)
)
finally:
await session.close()

async def send_messages(self) -> None:
async def _concurent_send_messages():
Expand All @@ -66,4 +68,3 @@ async def _concurent_send_messages():
await asyncio.gather(*tasks)

await _concurent_send_messages()
await self.session.close()

0 comments on commit b9a7ba4

Please sign in to comment.