close web-session on app close

This commit is contained in:
Oleg A 2024-08-06 12:04:26 +03:00
parent 3ba724e2fa
commit a7711cc2e0
No known key found for this signature in database
GPG key ID: 5FE046817A9657C5
9 changed files with 37 additions and 2 deletions

View file

@ -52,6 +52,9 @@ async def main() -> None:
# And the run events dispatching
await dp.start_polling(bot)
# Don't forget to close web-session on your app close
await bot.session.close()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

View file

@ -71,14 +71,20 @@ async def on_startup(bot: Bot) -> None:
await bot.set_webhook(f"{BASE_WEBHOOK_URL}{WEBHOOK_PATH}", secret_token=WEBHOOK_SECRET)
async def on_shutdown(bot: Bot) -> None:
# Gracefully close web-sessions on app shutdown
await bot.session.close()
def main() -> None:
# Dispatcher is a root router
dp = Dispatcher()
# ... and all other routers should be attached to Dispatcher
dp.include_router(router)
# Register startup hook to initialize webhook
# Register startup and shutdown hooks
dp.startup.register(on_startup)
dp.shutdown.register(on_shutdown)
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))

View file

@ -82,14 +82,20 @@ async def on_startup(bot: Bot) -> None:
)
async def on_shutdown(bot: Bot) -> None:
# Gracefully close web-sessions on app shutdown
await bot.session.close()
def main() -> None:
# Dispatcher is a root router
dp = Dispatcher()
# ... and all other routers should be attached to Dispatcher
dp.include_router(router)
# Register startup hook to initialize webhook
# Register startup and shutdown hooks
dp.startup.register(on_startup)
dp.shutdown.register(on_shutdown)
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))

View file

@ -104,9 +104,13 @@ async def handle_set_name(message: types.Message, command: CommandObject) -> Non
async def main() -> None:
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# And the run events dispatching
await dp.start_polling(bot)
# Don't forget to close web-session on your app close
await bot.session.close()
if __name__ == "__main__":
loop = asyncio.new_event_loop()

View file

@ -135,6 +135,9 @@ async def main():
# Start event dispatching
await dp.start_polling(bot)
# Don't forget to close web-session on your app close
await bot.session.close()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

View file

@ -58,6 +58,11 @@ async def on_startup(dispatcher: Dispatcher, bot: Bot):
await bot.set_webhook(f"{BASE_URL}{MAIN_BOT_PATH}")
async def on_shutdown(dispatcher: Dispatcher, bot: Bot) -> None:
# Gracefully close web-sessions on app shutdown
await bot.session.close()
def main():
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
session = AiohttpSession()
@ -70,6 +75,7 @@ def main():
main_dispatcher = Dispatcher(storage=storage)
main_dispatcher.include_router(main_router)
main_dispatcher.startup.register(on_startup)
main_dispatcher.shutdown.register(on_shutdown)
multibot_dispatcher = Dispatcher(storage=storage)
multibot_dispatcher.include_router(form_router)

View file

@ -292,6 +292,7 @@ async def main():
dp = create_dispatcher()
bot = Bot(token=TOKEN)
await dp.start_polling(bot)
await bot.session.close()
if __name__ == "__main__":

View file

@ -91,6 +91,9 @@ async def main() -> None:
# Start event dispatching
await dp.start_polling(bot)
# Don't forget to close web-session on your app close
await bot.session.close()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

View file

@ -58,6 +58,9 @@ async def main() -> None:
await dispatcher.start_polling(bot)
# Don't forget to close web-session on your app close
await bot.session.close()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)