From d9b4556795152f7d984b53cc0aa4f4ba65637963 Mon Sep 17 00:00:00 2001 From: Dmitry Afanasyev Date: Sat, 20 Aug 2022 02:36:13 +0300 Subject: [PATCH] add async queue --- app/main.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 3523f2c..1e58c05 100644 --- a/app/main.py +++ b/app/main.py @@ -2,6 +2,7 @@ import asyncio import sys from http import HTTPStatus from pathlib import Path +from typing import Any from aiogram import Bot, Dispatcher from aiogram.types import Update @@ -22,11 +23,14 @@ from app.settings import ( WEBHOOK_URL, ) +queue: Any = asyncio.Queue() + async def bot_startup() -> None: await bot.set_webhook(WEBHOOK_URL) logger.info(f'Webhook set to {WEBHOOK_URL}'.replace(API_TOKEN, '{BOT_API_TOKEN}')) asyncio_schedule() + await worker() async def bot_shutdown() -> None: @@ -83,13 +87,17 @@ async def webhook(request: web.Request) -> web.Response: """ data = await request.json() tg_update = Update(**data) + queue.put_nowait(tg_update) + return web.Response(status=HTTPStatus.ACCEPTED) + +async def worker() -> None: Dispatcher.set_current(dispatcher) Bot.set_current(dispatcher.bot) - await dispatcher.process_update(tg_update) - - return web.Response(status=HTTPStatus.OK) + while True: + update = await queue.get() + await dispatcher.process_update(update) async def create_app() -> web.Application: