{"id":347741,"date":"2023-05-22T03:00:21","date_gmt":"2023-05-22T03:00:21","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=347741"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=347741","title":{"rendered":"<span>\u0425\u043e\u0440\u043e\u0448\u0430\u044f \u0437\u0430\u043c\u0435\u043d\u0430 Celery<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<p>\u0412 \u0441\u0432\u043e\u0435\u0439 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 &#171;<a href=\"https:\/\/habr.com\/ru\/articles\/721186\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u043f\u043e\u0434\u0440\u0443\u0436\u0438\u0442\u044c Celery \u0438 SQLAlchemy 2.0 \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c Python<\/a>&#187; \u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u043b \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 &#171;\u0438\u0437-\u043f\u043e\u0434 Celery&#187; \u0438 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043c\u043d\u0435 \u0441\u043e\u043e\u0431\u0449\u0438\u043b\u0438 \u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0438 \u0435\u0449\u0451 \u043e\u0434\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c aio_pika. \u0418 \u043f\u0440\u0438\u0437\u043d\u0430\u0442\u044c\u0441\u044f, \u043e \u043d\u0435\u0439 \u044f \u0440\u0430\u043d\u044c\u0448\u0435 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0441\u043b\u044b\u0448\u0430\u043b. \u041e\u043d\u043e \u0438 \u043d\u0435 \u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u0441\u0435\u0433\u043e \u0432 \u0440\u0430\u0439\u043e\u043d\u0435 1\u041a \u0437\u0432\u0451\u0437\u0434 \u043d\u0430 GitHub (\u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 20\u041a+ \u0443 Celery). \u042f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0432\u0441\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0435 (500+ \u0437\u0432\u0451\u0437\u0434) \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u044d\u0442\u043e\u043c \u0438\u0437-\u0437\u0430 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0439 (\u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442) \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0421\u0442\u0435\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435: FastAPI, RabbitMQ, aio_pika \u0438 docker. \u0421\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0442\u0435\u043c \u043a\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 Celery \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0442\u0435\u043c, \u043a\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u044b\u0448\u0430\u043b \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 RabbitMQ.<\/p>\n<p>\u041d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f:<\/p>\n<ol>\n<li>\n<p><a href=\"#rabbit\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f RabbitMQ<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#router\" rel=\"noopener noreferrer nofollow\">Task router \u0434\u043b\u044f consumer&#8217;a<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#consumer\" rel=\"noopener noreferrer nofollow\">\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 consumer&#8217;a<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#main\" rel=\"noopener noreferrer nofollow\">\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<\/ol>\n<h2>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h2>\n<p>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u0438\u0440\u0443\u0435\u0442 \u0441\u0435\u0431\u044f &#171;\u043e\u0431\u0451\u0440\u0442\u043a\u043e\u0439 aiormq&nbsp;\u0434\u043b\u044f&nbsp;asyncio \u0434\u043b\u044f \u043b\u044e\u0434\u0435\u0439&#187;. \u041c\u043e\u0435\u0439 \u0446\u0435\u043b\u044c\u044e \u0441\u0442\u0430\u043b\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c Celery, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u043d\u0430 \u043d\u0435\u0451. \u0420\u0435\u0448\u0438\u043b \u044f \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043d\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 worker&#8217;\u043e\u0432 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b, \u0447\u0435\u0433\u043e \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b. \u0412\u0442\u043e\u0440\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c\u0438 \u0441\u0442\u0430\u043b\u0438: \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438, \u0437\u0430\u043f\u0430\u0445 legacy (\u044f \u043f\u0440\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442 self, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0439) \u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 type-\u0445\u0438\u043d\u0442\u043e\u0432 (\u044d\u0442\u043e \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u043c\u0435\u0441\u0442\u0435 \u0432\u0430\u0436\u043d\u043e\u0441\u0442\u0438!). Celery \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0434\u043b\u044f IO-Bound \u0438 Delay \u0437\u0430\u0434\u0430\u0447, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 \u0431\u044b\u043b\u0430 \u043e\u0447\u0435\u043d\u044c \u043a\u0441\u0442\u0430\u0442\u0438.<\/p>\n<p><a class=\"anchor\" name=\"rabbit\" id=\"rabbit\"><\/a><\/p>\n<h2>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f RabbitMQ<\/h2>\n<p>\u042f \u043e\u0431\u043d\u043e\u0432\u0438\u043b \u0441\u0432\u043e\u0439 RabbitMQ \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043f\u043b\u0430\u0433\u0438\u043d <a href=\"https:\/\/github.com\/rabbitmq\/rabbitmq-delayed-message-exchange\" rel=\"noopener noreferrer nofollow\">&#171;RabbitMQ Delayed Message Plugin&#187;<\/a>. \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0431\u044b\u043b \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u0435\u043b\u0430\u0442\u044c &#171;\u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435&#187; \u0437\u0430\u0434\u0430\u0447\u0438. \u0422.\u0435. \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u044b\u043b\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u043f\u043e \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. Celery \u0441 \u044d\u0442\u0438\u043c \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u043b\u0441\u044f, \u0442.\u043a. \u0443 \u043d\u0435\u0433\u043e \u0431\u044b\u043b\u0430 \u043d\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0434\u0430\u043d\u043d\u043e\u0439 \u0444\u0438\u0447\u0438, \u043d\u043e, \u043a\u0430\u043a \u044f \u043f\u043e\u043d\u044f\u043b, <code>aio-pika<\/code> \u0442\u0430\u043a\u043e\u0433\u043e \u043d\u0435 \u0438\u043c\u0435\u0435\u0442. \u042d\u0442\u043e\u0442 \u043f\u043b\u0430\u0433\u0438\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0432 \u0441\u0430\u043c RabbitMQ. \u041c\u043e\u0439 docker-compose \u043a\u043e\u043d\u0444\u0438\u0433 \u0441\u0442\u0430\u043b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<details class=\"spoiler\">\n<summary>docker-compose.yaml<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"yaml\"> rabbit:     image: rabbitmq:3-management     hostname: rabbit     env_file:       - .env     volumes:       - .\/services\/rabbit\/delayed_message.ez:\/opt\/rabbitmq\/plugins\/delayed_message.ez       - .\/services\/rabbit\/enabled:\/etc\/rabbitmq\/enabled_plugins     ports:       - \"15672:15672\"<\/code><\/pre>\n<p>\u0427\u0435\u0440\u0435\u0437 volumes \u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b \u0441\u043a\u0430\u0447\u0430\u043d\u043d\u044b\u0439 \u043f\u043b\u0430\u0433\u0438\u043d, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0435\u0433\u043e \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e. \u041c\u043e\u0439 enabled_plugins \u0444\u0430\u0439\u043b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code>[rabbitmq_delayed_message_exchange,rabbitmq_management,rabbitmq_prometheus].<\/code><\/pre>\n<p><em><sub>*\u0422\u043e\u0447\u043a\u0430 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u0430<\/sub><\/em><\/p>\n<\/div>\n<\/details>\n<p><a class=\"anchor\" name=\"router\" id=\"router\"><\/a><\/p>\n<h2>Task router \u0434\u043b\u044f consumer&#8217;a<\/h2>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u044d\u0442\u0430\u043f\u043e\u043c \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b Router \u0434\u043b\u044f \u043c\u043e\u0435\u0433\u043e worker&#8217;\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0431\u044b \u0434\u043b\u044f \u043c\u0435\u043d\u044f \u0443\u0434\u043e\u0431\u0435\u043d. \u041d\u0430 \u044d\u0442\u043e\u043c \u043c\u043e\u043c\u0435\u043d\u0442\u0435 \u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0437\u0430\u043c\u043e\u0440\u043e\u0447\u0438\u043b\u0441\u044f:<\/p>\n<details class=\"spoiler\">\n<summary>router.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">class Router:     _routes: dict[str, list[str]] = {}      def __init__(self):         modules = list(filter(             lambda x: x != '__init__',             map(lambda y: y.split('.')[0], os.listdir('tasks'))         ))         for module in modules:             imported = import_module(f'tasks.{module}')             if not hasattr(imported, '__all__'):                 continue             self._routes[module] = imported.__all__             del imported     def get_method(self, action: str) -&gt; Optional[Callable]:         module = action.split(':')[0] # \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430         method = action.split(':')[1] # \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438         if self._exists(module, method):             return getattr(import_module(f'tasks.{module}'), method)<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f _router \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u0432 \u043f\u0430\u043f\u043a\u0435 tasks, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043b\u0435\u0436\u0430\u0442 \u0441\u0430\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u0437\u0430\u0434\u0430\u0447\u0438). \u0422\u0430\u043a \u0436\u0435 \u043e\u043d\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <strong>all<\/strong> \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430. \u0414\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">async def test(is_test: bool):     print(f'Hello world! Value is: {is_test}')  __all__ = ['test']<\/code><\/pre>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u044f\u043b\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u043c\u0435\u044e\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u042f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0440\u043e\u0443\u0442\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0433 \u0431\u044b \u0443\u0447\u0435\u0441\u0442\u044c \u0438 \u044d\u0442\u043e:<\/p>\n<details class=\"spoiler\">\n<summary>router.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">def check_args(func: Callable, data: dict) -&gt; bool:     hints = get_type_hints(func)     for arg, arg_type in hints.items():         if arg not in data:             return False         if not isinstance(data[arg], arg_type):             return False     return True<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0435\u0439 \u043f\u043e\u0434\u0441\u0443\u043d\u0443\u0442\u044c. \u041c\u044b \u0442\u0430\u043a \u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0442\u0438\u043f\u044b \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0415\u0441\u043b\u0438 \u0432\u0441\u0451 \u043e\u043a &#8212; \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c True<\/p>\n<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u044f \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043b \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \\ \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c \u0444\u0430\u0439\u043b\u043e\u0432 \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 tasks. \u042d\u0442\u043e \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0438 \u0433\u0438\u0431\u043a\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c.<\/p>\n<\/p>\n<p><a class=\"anchor\" name=\"consumer\" id=\"consumer\"><\/a><\/p>\n<h2>\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 consumer&#8217;a<\/h2>\n<details class=\"spoiler\">\n<summary>consumer.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\"> async def process_message(message: AbstractIncomingMessage):     async with message.process():         message = MessageSchema.parse_obj(json.loads(message.body.decode()))         method = router.get_method(message.action) # \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e         if method:             if not router.check_args(method, message.body): # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c                 print('Invalid args')                 return             if inspect.iscoroutinefunction(method): # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f async \u0438\u043b\u0438 \u043d\u0435\u0442                 await method(**message.body)             else:                 method(**message.body)   async def main() -&gt; None:     queue_key = rabbit_config.RABBITMQ_QUEUE      connection = await aio_pika.connect_robust(rabbit_config.url)     # \u0414\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 RabbitMQ \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c publisher_confirms=False     channel = await connection.channel(publisher_confirms=False)     # \u041a\u043e\u043b-\u0432\u043e \u0437\u0430\u0434\u0430\u0447, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 consumer \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 100     await channel.set_qos(prefetch_count=100)     queue = await channel.declare_queue(queue_key)          exchange = await channel.declare_exchange(         # \u041e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c exchange \u0441 \u0438\u043c\u0435\u043d\u0435\u043c main \u0438 \u0442\u0438\u043f\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438         # \u0412\u0430\u0436\u043d\u043e \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u043e \u0438\u043c\u044f (main) \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u043b\u043e \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 publisher         'main', ExchangeType.X_DELAYED_MESSAGE,          arguments={             'x-delayed-type': 'direct'         }     )     await queue.bind(exchange, queue_key)     await queue.consume(process_message)     try:         await asyncio.Future()     finally:         await connection.close()   if __name__ == \"__main__\":     asyncio.run(main()) <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c \u043d\u0430 \u044d\u0442\u043e\u043c \u0441\u0442\u043e\u0440\u043e\u043d\u0430 consumer&#8217;a \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u0430 \u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0433\u043e \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0440\u0430 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 (publisher).<\/p>\n<p><a class=\"anchor\" name=\"main\" id=\"main\"><\/a><\/p>\n<h2>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u0441\u043d\u043e\u0432\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u041e\u041e\u041f \u0438 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 aio-pika, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u043a\u0440\u044b\u043b \u043c\u043e\u0438 \u043d\u0443\u0436\u0434\u044b. \u0415\u0433\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u043b\u0430 \u0432 \u043d\u043e\u0432\u0435\u043d\u044c\u043a\u043e\u043c lifespan (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043a\u043e\u0440\u043e \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u044b\u0442\u043e\u043b\u043a\u043d\u0435\u0442 \u0441\u0442\u0430\u0440\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b):<\/p>\n<pre><code class=\"python\">@asynccontextmanager async def lifespan(_: FastAPI):     await rabbit_connection.connect()     yield     await rabbit_connection.disconnect()  app = FastAPI(lifespan=lifespan)<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<details class=\"spoiler\">\n<summary>rabbit_connection.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">class RabbitConnection:     _connection: AbstractRobustConnection | None = None     _channel: AbstractRobustChannel | None = None     _exchange: AbstractRobustExchange | None = None      async def disconnect(self) -&gt; None:         if self._channel and not self._channel.is_closed:             await self._channel.close()         if self._connection and not self._connection.is_closed:             await self._connection.close()         self._connection = None         self._channel = None      async def connect(self) -&gt; None:         try:             self._connection = await connect_robust(rabbit_config.url)             self._channel = await self._connection.channel(publisher_confirms=False)             self._exchange = await self._channel.declare_exchange(                 # \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0438\u0437 consumer'a. \u0412\u0430\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0435                 # \u0438\u043c\u044f exchange'\u043e\u0432. \u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 `main`                 'main', ExchangeType.X_DELAYED_MESSAGE,                 arguments={                     'x-delayed-type': 'direct'                 }             )         except Exception as e:             await self.disconnect()      async def send_messages(             self,             messages: list[MessageSchema],             *,             routing_key: str = rabbit_config.RABBITMQ_QUEUE,             delay: int = None # \u0417\u0430\u0434\u0435\u0440\u0436\u043a\u0430, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 (\u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445)     ) -&gt; None:         async with self._channel.transaction():             headers = None             if delay:                 headers = {                     'x-delay': f'{delay * 1000}' # \u042d\u0442\u043e \u0442\u043e\u0436\u0435 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u0434\u043b\u044f RabbitMQ                 }             for message in messages:                 message = Message(                     body=json.dumps(message.dict()).encode(),                     headers=headers                 )                 await self._exchange.publish(                     message,                     routing_key=routing_key,                     mandatory=False if delay else True # \u0427\u0442\u043e\u0431\u044b \u0432 \u043b\u043e\u0433\u0430\u0445 \u0431\u044b\u043b \u043f\u043e\u0440\u044f\u0434\u043e\u043a ;)                 )   rabbit_connection = RabbitConnection()<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u043a\u0438 worker&#8217;\u0443 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u043b\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<details class=\"spoiler\">\n<summary>main.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">@router.get('\/test') async def test():     message = MessageSchema(         action='images:delete',         body={'path': 'assets\/temp\/temp.png'}     )     await rabbit_connection.send_messages(       [message for _ in range(150)],        delay=20     )     return {'status': 'published'}<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u043e\u0434\u0432\u043e\u0434\u044f \u0438\u0442\u043e\u0433\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0447\u0442\u043e worker \u0442\u0435\u043f\u0435\u0440\u044c \u0447\u0443\u0432\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0435\u0431\u044f \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u0435\u0435 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0441\u0442\u0430\u0442\u044c\u044f \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439. \u0412\u0441\u0435\u043c \u0441\u043f\u0430\u0441\u0438\u0431\u043e, \u0432\u0441\u0435\u043c \u043f\u043e\u043a\u0430.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/736598\/\"> https:\/\/habr.com\/ru\/articles\/736598\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<p>\u0412 \u0441\u0432\u043e\u0435\u0439 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 &#171;<a href=\"https:\/\/habr.com\/ru\/articles\/721186\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u043f\u043e\u0434\u0440\u0443\u0436\u0438\u0442\u044c Celery \u0438 SQLAlchemy 2.0 \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c Python<\/a>&#187; \u044f \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u043b \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 &#171;\u0438\u0437-\u043f\u043e\u0434 Celery&#187; \u0438 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043c\u043d\u0435 \u0441\u043e\u043e\u0431\u0449\u0438\u043b\u0438 \u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0438 \u0435\u0449\u0451 \u043e\u0434\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c aio_pika. \u0418 \u043f\u0440\u0438\u0437\u043d\u0430\u0442\u044c\u0441\u044f, \u043e \u043d\u0435\u0439 \u044f \u0440\u0430\u043d\u044c\u0448\u0435 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0441\u043b\u044b\u0448\u0430\u043b. \u041e\u043d\u043e \u0438 \u043d\u0435 \u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u0441\u0435\u0433\u043e \u0432 \u0440\u0430\u0439\u043e\u043d\u0435 1\u041a \u0437\u0432\u0451\u0437\u0434 \u043d\u0430 GitHub (\u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 20\u041a+ \u0443 Celery). \u042f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0432\u0441\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0435 (500+ \u0437\u0432\u0451\u0437\u0434) \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u044d\u0442\u043e\u043c \u0438\u0437-\u0437\u0430 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0439 (\u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442) \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0421\u0442\u0435\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435: FastAPI, RabbitMQ, aio_pika \u0438 docker. \u0421\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0442\u0435\u043c \u043a\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 Celery \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0442\u0435\u043c, \u043a\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u044b\u0448\u0430\u043b \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 RabbitMQ.<\/p>\n<p>\u041d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f:<\/p>\n<ol>\n<li>\n<p><a href=\"#rabbit\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f RabbitMQ<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#router\" rel=\"noopener noreferrer nofollow\">Task router \u0434\u043b\u044f consumer&#8217;a<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#consumer\" rel=\"noopener noreferrer nofollow\">\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 consumer&#8217;a<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#main\" rel=\"noopener noreferrer nofollow\">\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<\/ol>\n<h2>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h2>\n<p>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u043e\u043d\u0438\u0440\u0443\u0435\u0442 \u0441\u0435\u0431\u044f &#171;\u043e\u0431\u0451\u0440\u0442\u043a\u043e\u0439 aiormq&nbsp;\u0434\u043b\u044f&nbsp;asyncio \u0434\u043b\u044f \u043b\u044e\u0434\u0435\u0439&#187;. \u041c\u043e\u0435\u0439 \u0446\u0435\u043b\u044c\u044e \u0441\u0442\u0430\u043b\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c Celery, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u043d\u0430 \u043d\u0435\u0451. \u0420\u0435\u0448\u0438\u043b \u044f \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043d\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 worker&#8217;\u043e\u0432 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b, \u0447\u0435\u0433\u043e \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b. \u0412\u0442\u043e\u0440\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c\u0438 \u0441\u0442\u0430\u043b\u0438: \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438, \u0437\u0430\u043f\u0430\u0445 legacy (\u044f \u043f\u0440\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442 self, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0439) \u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 type-\u0445\u0438\u043d\u0442\u043e\u0432 (\u044d\u0442\u043e \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u043c\u0435\u0441\u0442\u0435 \u0432\u0430\u0436\u043d\u043e\u0441\u0442\u0438!). Celery \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0434\u043b\u044f IO-Bound \u0438 Delay \u0437\u0430\u0434\u0430\u0447, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 \u0431\u044b\u043b\u0430 \u043e\u0447\u0435\u043d\u044c \u043a\u0441\u0442\u0430\u0442\u0438.<\/p>\n<p><a class=\"anchor\" name=\"rabbit\" id=\"rabbit\"><\/a><\/p>\n<h2>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f RabbitMQ<\/h2>\n<p>\u042f \u043e\u0431\u043d\u043e\u0432\u0438\u043b \u0441\u0432\u043e\u0439 RabbitMQ \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043f\u043b\u0430\u0433\u0438\u043d <a href=\"https:\/\/github.com\/rabbitmq\/rabbitmq-delayed-message-exchange\" rel=\"noopener noreferrer nofollow\">&#171;RabbitMQ Delayed Message Plugin&#187;<\/a>. \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0431\u044b\u043b \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u0435\u043b\u0430\u0442\u044c &#171;\u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435&#187; \u0437\u0430\u0434\u0430\u0447\u0438. \u0422.\u0435. \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u044b\u043b\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u043f\u043e \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. Celery \u0441 \u044d\u0442\u0438\u043c \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u043b\u0441\u044f, \u0442.\u043a. \u0443 \u043d\u0435\u0433\u043e \u0431\u044b\u043b\u0430 \u043d\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0434\u0430\u043d\u043d\u043e\u0439 \u0444\u0438\u0447\u0438, \u043d\u043e, \u043a\u0430\u043a \u044f \u043f\u043e\u043d\u044f\u043b, <code>aio-pika<\/code> \u0442\u0430\u043a\u043e\u0433\u043e \u043d\u0435 \u0438\u043c\u0435\u0435\u0442. \u042d\u0442\u043e\u0442 \u043f\u043b\u0430\u0433\u0438\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0432 \u0441\u0430\u043c RabbitMQ. \u041c\u043e\u0439 docker-compose \u043a\u043e\u043d\u0444\u0438\u0433 \u0441\u0442\u0430\u043b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<details class=\"spoiler\">\n<summary>docker-compose.yaml<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"yaml\"> rabbit:     image: rabbitmq:3-management     hostname: rabbit     env_file:       - .env     volumes:       - .\/services\/rabbit\/delayed_message.ez:\/opt\/rabbitmq\/plugins\/delayed_message.ez       - .\/services\/rabbit\/enabled:\/etc\/rabbitmq\/enabled_plugins     ports:       - \"15672:15672\"<\/code><\/pre>\n<p>\u0427\u0435\u0440\u0435\u0437 volumes \u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b \u0441\u043a\u0430\u0447\u0430\u043d\u043d\u044b\u0439 \u043f\u043b\u0430\u0433\u0438\u043d, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0435\u0433\u043e \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e. \u041c\u043e\u0439 enabled_plugins \u0444\u0430\u0439\u043b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code>[rabbitmq_delayed_message_exchange,rabbitmq_management,rabbitmq_prometheus].<\/code><\/pre>\n<p><em><sub>*\u0422\u043e\u0447\u043a\u0430 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u0430<\/sub><\/em><\/p>\n<\/div>\n<\/details>\n<p><a class=\"anchor\" name=\"router\" id=\"router\"><\/a><\/p>\n<h2>Task router \u0434\u043b\u044f consumer&#8217;a<\/h2>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u044d\u0442\u0430\u043f\u043e\u043c \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b Router \u0434\u043b\u044f \u043c\u043e\u0435\u0433\u043e worker&#8217;\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0431\u044b \u0434\u043b\u044f \u043c\u0435\u043d\u044f \u0443\u0434\u043e\u0431\u0435\u043d. \u041d\u0430 \u044d\u0442\u043e\u043c \u043c\u043e\u043c\u0435\u043d\u0442\u0435 \u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0437\u0430\u043c\u043e\u0440\u043e\u0447\u0438\u043b\u0441\u044f:<\/p>\n<details class=\"spoiler\">\n<summary>router.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">class Router:     _routes: dict[str, list[str]] = {}      def __init__(self):         modules = list(filter(             lambda x: x != '__init__',             map(lambda y: y.split('.')[0], os.listdir('tasks'))         ))         for module in modules:             imported = import_module(f'tasks.{module}')             if not hasattr(imported, '__all__'):                 continue             self._routes[module] = imported.__all__             del imported     def get_method(self, action: str) -&gt; Optional[Callable]:         module = action.split(':')[0] # \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430         method = action.split(':')[1] # \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438         if self._exists(module, method):             return getattr(import_module(f'tasks.{module}'), method)<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f _router \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u0432 \u043f\u0430\u043f\u043a\u0435 tasks, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043b\u0435\u0436\u0430\u0442 \u0441\u0430\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u0437\u0430\u0434\u0430\u0447\u0438). \u0422\u0430\u043a \u0436\u0435 \u043e\u043d\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <strong>all<\/strong> \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430. \u0414\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">async def test(is_test: bool):     print(f'Hello world! Value is: {is_test}')  __all__ = ['test']<\/code><\/pre>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u044f\u043b\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u043c\u0435\u044e\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u042f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0440\u043e\u0443\u0442\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0433 \u0431\u044b \u0443\u0447\u0435\u0441\u0442\u044c \u0438 \u044d\u0442\u043e:<\/p>\n<details class=\"spoiler\">\n<summary>router.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">def check_args(func: Callable, data: dict) -&gt; bool:     hints = get_type_hints(func)     for arg, arg_type in hints.items():         if arg not in data:             return False         if not isinstance(data[arg], arg_type):             return False     return True<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0435\u0439 \u043f\u043e\u0434\u0441\u0443\u043d\u0443\u0442\u044c. \u041c\u044b \u0442\u0430\u043a \u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0442\u0438\u043f\u044b \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0415\u0441\u043b\u0438 \u0432\u0441\u0451 \u043e\u043a &#8212; \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c True<\/p>\n<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u044f \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043b \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \\ \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c \u0444\u0430\u0439\u043b\u043e\u0432 \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 tasks. \u042d\u0442\u043e \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0438 \u0433\u0438\u0431\u043a\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c.<\/p>\n<\/p>\n<p><a class=\"anchor\" name=\"consumer\" id=\"consumer\"><\/a><\/p>\n<h2>\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 consumer&#8217;a<\/h2>\n<details class=\"spoiler\">\n<summary>consumer.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\"> async def process_message(message: AbstractIncomingMessage):     async with message.process():         message = MessageSchema.parse_obj(json.loads(message.body.decode()))         method = router.get_method(message.action) # \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e         if method:             if not router.check_args(method, message.body): # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c                 print('Invalid args')                 return             if inspect.iscoroutinefunction(method): # \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f async \u0438\u043b\u0438 \u043d\u0435\u0442                 await method(**message.body)             else:                 method(**message.body)   async def main() -&gt; None:     queue_key = rabbit_config.RABBITMQ_QUEUE      connection = await aio_pika.connect_robust(rabbit_config.url)     # \u0414\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 RabbitMQ \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c publisher_confirms=False     channel = await connection.channel(publisher_confirms=False)     # \u041a\u043e\u043b-\u0432\u043e \u0437\u0430\u0434\u0430\u0447, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 consumer \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 100     await channel.set_qos(prefetch_count=100)     queue = await channel.declare_queue(queue_key)          exchange = await channel.declare_exchange(         # \u041e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c exchange \u0441 \u0438\u043c\u0435\u043d\u0435\u043c main \u0438 \u0442\u0438\u043f\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438         # \u0412\u0430\u0436\u043d\u043e \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u043e \u0438\u043c\u044f (main) \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u043b\u043e \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 publisher         'main', ExchangeType.X_DELAYED_MESSAGE,          arguments={             'x-delayed-type': 'direct'         }     )     await queue.bind(exchange, queue_key)     await queue.consume(process_message)     try:         await asyncio.Future()     finally:         await connection.close()   if __name__ == \"__main__\":     asyncio.run(main()) <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c \u043d\u0430 \u044d\u0442\u043e\u043c \u0441\u0442\u043e\u0440\u043e\u043d\u0430 consumer&#8217;a \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u0430 \u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0433\u043e \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0440\u0430 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 (publisher).<\/p>\n<p><a class=\"anchor\" name=\"main\" id=\"main\"><\/a><\/p>\n<h2>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u0441\u043d\u043e\u0432\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u041e\u041e\u041f \u0438 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 aio-pika, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u043a\u0440\u044b\u043b \u043c\u043e\u0438 \u043d\u0443\u0436\u0434\u044b. \u0415\u0433\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u043b\u0430 \u0432 \u043d\u043e\u0432\u0435\u043d\u044c\u043a\u043e\u043c lifespan (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043a\u043e\u0440\u043e \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u044b\u0442\u043e\u043b\u043a\u043d\u0435\u0442 \u0441\u0442\u0430\u0440\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b):<\/p>\n<pre><code class=\"python\">@asynccontextmanager async def lifespan(_: FastAPI):     await rabbit_connection.connect()     yield     await rabbit_connection.disconnect()  app = FastAPI(lifespan=lifespan)<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<details class=\"spoiler\">\n<summary>rabbit_connection.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">class RabbitConnection:     _connection: AbstractRobustConnection | None = None     _channel: AbstractRobustChannel | None = None     _exchange: AbstractRobustExchange | None = None      async def disconnect(self) -&gt; None:         if self._channel and not self._channel.is_closed:             await self._channel.close()         if self._connection and not self._connection.is_closed:             await self._connection.close()         self._connection = None         self._channel = None      async def connect(self) -&gt; None:         try:             self._connection = await connect_robust(rabbit_config.url)             self._channel = await self._connection.channel(publisher_confirms=False)             self._exchange = await self._channel.declare_exchange(                 # \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0438\u0437 consumer'a. \u0412\u0430\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0435                 # \u0438\u043c\u044f exchange'\u043e\u0432. \u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 `main`                 'main', ExchangeType.X_DELAYED_MESSAGE,                 arguments={                     'x-delayed-type': 'direct'                 }             )         except Exception as e:             await self.disconnect()      async def send_messages(             self,             messages: list[MessageSchema],             *,             routing_key: str = rabbit_config.RABBITMQ_QUEUE,             delay: int = None # \u0417\u0430\u0434\u0435\u0440\u0436\u043a\u0430, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 (\u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445)     ) -&gt; None:         async with self._channel.transaction():             headers = None             if delay:                 headers = {                     'x-delay': f'{delay * 1000}' # \u042d\u0442\u043e \u0442\u043e\u0436\u0435 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u0434\u043b\u044f RabbitMQ                 }             for message in messages:                 message = Message(                     body=json.dumps(message.dict()).encode(),                     headers=headers                 )                 await self._exchange.publish(                     message,                     routing_key=routing_key,                     mandatory=False if delay else True # \u0427\u0442\u043e\u0431\u044b \u0432 \u043b\u043e\u0433\u0430\u0445 \u0431\u044b\u043b \u043f\u043e\u0440\u044f\u0434\u043e\u043a ;)                 )   rabbit_connection = RabbitConnection()<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u043a\u0438 worker&#8217;\u0443 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u043b\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<details class=\"spoiler\">\n<summary>main.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">@router.get('\/test') async def test():     message = MessageSchema(         action='images:delete',         body={'path': 'assets\/temp\/temp.png'}     )     await rabbit_connection.send_messages(       [message for _ in range(150)],        delay=20     )     return {'status': 'published'}<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041f\u043e\u0434\u0432\u043e\u0434\u044f \u0438\u0442\u043e\u0433\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0447\u0442\u043e worker \u0442\u0435\u043f\u0435\u0440\u044c \u0447\u0443\u0432\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0435\u0431\u044f \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u0435\u0435 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0441\u0442\u0430\u0442\u044c\u044f \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439. \u0412\u0441\u0435\u043c \u0441\u043f\u0430\u0441\u0438\u0431\u043e, \u0432\u0441\u0435\u043c \u043f\u043e\u043a\u0430.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/736598\/\"> https:\/\/habr.com\/ru\/articles\/736598\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-347741","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/347741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=347741"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/347741\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=347741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=347741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=347741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}