{"id":357611,"date":"2024-05-20T23:54:28","date_gmt":"2024-05-20T23:54:28","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=357611"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=357611","title":{"rendered":"<span>\u041f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 Python Asyncio: \u0418\u0441\u0447\u0435\u0440\u043f\u044b\u0432\u0430\u044e\u0449\u0435\u0435 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e<\/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<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u00a0\u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0441\u00a0\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0438\u043b\u0438\u00a0\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c\u0438 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043e\u00a0\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0433\u043e\u043d\u043a\u0438 \u043f\u0440\u0438\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0435\u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u0414\u0430\u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043b\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u043d\u0430\u00a0\u0435\u0434\u0438\u043d\u0438\u0446\u0443 \u0432\u00a0\u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u0441\u00a0\u0442\u0440\u0443\u0434\u043e\u043c \u043f\u043e\u0434\u0434\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044e. \u041d\u043e\u00a0\u043f\u0440\u0438\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 asyncio \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0432\u00a0\u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 (\u0435\u0441\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u044f\u0432\u043d\u043e \u043d\u0435\u00a0\u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043b\u0438 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0438\u043b\u0438\u00a0\u043c\u043d\u043e\u0433\u043e\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438), \u0437\u043d\u0430\u0447\u0438\u0442, \u043c\u043e\u0436\u043d\u043e \u043d\u0435\u00a0\u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u044c\u0441\u044f \u043e\u00a0\u0433\u043e\u043d\u043a\u0430\u0445, \u043f\u0440\u0430\u0432\u0434\u0430? \u041d\u0430\u00a0\u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0441\u0435 \u043d\u0435\u00a0\u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e!<\/p>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438, \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0432\u00a0\u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0445 \u0438 \u043c\u043d\u043e\u0433\u043e\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445, \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u044b \u0432\u00a0\u0441\u0438\u043b\u0443 \u043e\u0434\u043d\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u043f\u0440\u0438\u0440\u043e\u0434\u044b asyncio, \u043d\u043e\u00a0\u044d\u0442\u043e \u043d\u0435\u00a0\u0441\u043e\u0432\u0441\u0435\u043c \u0442\u0430\u043a. \u0412\u0430\u043c \u0440\u0435\u0434\u043a\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0442\u044c \u043a\u00a0\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u00a0asyncio, \u043e\u0434\u043d\u0430\u043a\u043e \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0441\u00a0\u044d\u0442\u0438\u043c \u043d\u0443\u0436\u043d\u043e \u0447\u0442\u043e\u2011\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c. <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-sync.html\" rel=\"noopener noreferrer nofollow\"><strong>\u041f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 asyncio<\/strong><\/a> \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0434\u0435\u043b\u0438 \u043e\u0434\u043d\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0432\u044b \u0443\u0437\u043d\u0430\u0435\u0442\u0435, \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 asyncio, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e \u043b\u0443\u0447\u0448\u0438\u0445 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438. \u0412 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438.<\/p>\n<details class=\"spoiler\">\n<summary>readme<\/summary>\n<div class=\"spoiler__content\">\n<p><em>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u0438\u043b\u0438 \u0432\u044b \u043d\u0430\u0448\u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0443, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 Ctrl+Enter \u0438 \u044f \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044e. \u0421\u043f\u0430\u0441\u0438\u0431\u043e!<\/em><\/p>\n<\/div>\n<\/details>\n<h2>\u0417\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 asyncio?<\/h2>\n<p>\u041b\u044e\u0431\u043e\u0439, \u043a\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0432 Python, \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0431\u043b\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u043d\u0435\u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u044c\u044e, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 asyncio \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a, \u043d\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043b\u0438 \u0443 \u043d\u0435\u0433\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c\u044e? \u041e\u0442\u0432\u0435\u0442: \u043d\u0435\u0442.<\/p>\n<p>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 asyncio \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u043e\u0434\u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430, \u0430 \u0434\u0440\u0443\u0433\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u044d\u0442\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043e\u0448\u0438\u0431\u043e\u043a, \u0432 Python asyncio \u0432\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438, \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438. \u042d\u0442\u043e <strong>asyncio.Lock, asyncio.Semaphore, asyncio.Event<\/strong> \u0438 <strong>asyncio.Condition<\/strong>.<strong> <\/strong>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u0437\u0430\u0434\u0430\u0447, \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 asyncio \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0437\u0430\u0449\u0438\u0442\u0438\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432 \u0447\u0438\u0441\u043b\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043a \u043d\u0435\u043c\u0443 \u0437\u0430\u0434\u0430\u0447. <\/p>\n<h3>\u041f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 asyncio<\/h3>\n<h3>asyncio.Lock<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0443\u0436\u043d\u0430 \u043a\u043e\u043f\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0430\u0439\u0442\u0430. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442, \u0435\u0441\u0442\u044c \u043b\u0438 \u043e\u043d\u0430 \u0432 \u043a\u044d\u0448\u0435, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0435\u0435 \u0438\u0437 \u043a\u044d\u0448\u0430, \u0430 \u0435\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u0442 \u0435\u0435 \u0441 \u0441\u0430\u0439\u0442\u0430. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0447\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0430\u0439\u0442\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u044d\u0448\u0430, \u043f\u0440\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432\u0441\u0435 \u043e\u043d\u0438 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0442, \u0447\u0442\u043e \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u044d\u0448\u0435 \u043d\u0435\u0442, \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u044e\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u0434\u0435:<\/p>\n<pre><code class=\"python\">import asyncio import aiohttp  cache = dict()   async def request_remote():     print(\"Will request the website to get status.\")     async with aiohttp.ClientSession() as session:         response = await session.get(\"https:\/\/www.example.com\")         return response.status   async def get_value(key: str):     if key not in cache:         print(f\"The value of key {key} is not in cache.\")         value = await request_remote()         cache[key] = value     else:         print(f\"The value of key {key} is already in cache.\")         value = cache[key]     print(f\"The value of {key} is {value}\")     return value   async def main():     task_one = asyncio.create_task(get_value(\"status\"))     task_two = asyncio.create_task(get_value(\"status\"))      await asyncio.gather(task_one, task_two)   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/509\/b1d\/5bb\/509b1d5bbc3ee58c4a5148db2b1633a6.png\" alt=\"\u041e\u0431\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0447\u0438\u0442\u0430\u044e\u0442, \u0447\u0442\u043e \u0432 \u043a\u044d\u0448\u0435 \u043d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u043a \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u0441\u0430\u0439\u0442\u0443. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" title=\"\u041e\u0431\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0447\u0438\u0442\u0430\u044e\u0442, \u0447\u0442\u043e \u0432 \u043a\u044d\u0448\u0435 \u043d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u043a \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u0441\u0430\u0439\u0442\u0443. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" width=\"720\" height=\"184\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/509\/b1d\/5bb\/509b1d5bbc3ee58c4a5148db2b1633a6.png\"\/><\/p>\n<div><figcaption>\u041e\u0431\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0447\u0438\u0442\u0430\u044e\u0442, \u0447\u0442\u043e \u0432 \u043a\u044d\u0448\u0435 \u043d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u043a \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u0441\u0430\u0439\u0442\u0443. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<p>\u042d\u0442\u043e \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0430\u0448\u0435\u043c\u0443 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0437\u0430\u043c\u044b\u0441\u043b\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-sync.html#asyncio.Lock\" rel=\"noopener noreferrer nofollow\"><strong>asyncio.Lock<\/strong><\/a>. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u044d\u0448\u0435, \u043a\u043e\u0433\u0434\u0430 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u0435\u0440\u0432\u044b\u043c\u0438, \u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443, \u0431\u0443\u0434\u0443\u0442 \u0436\u0434\u0430\u0442\u044c.<\/p>\n<p>\u041f\u043e\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0430, \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0430\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443, \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u044d\u0448\u0430 \u0438 \u043d\u0435 \u0441\u043d\u0438\u043c\u0435\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p>\u0412\u0441\u044f \u0431\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u043d\u0438\u0436\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/050\/9c1\/334\/0509c1334ff90a8ec86b5bee8669fe7f.png\" alt=\"\u0412\u0441\u044f \u0431\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 asyncio.Lock. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" title=\"\u0412\u0441\u044f \u0431\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 asyncio.Lock. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" width=\"720\" height=\"472\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/050\/9c1\/334\/0509c1334ff90a8ec86b5bee8669fe7f.png\"\/><\/p>\n<div><figcaption>\u0412\u0441\u044f \u0431\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 asyncio.Lock. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u043c \u043d\u0430\u0448 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import Lock  import aiohttp  cache = dict() lock = Lock()   async def request_remote():     print(\"Will request the website to get status.\")     async with aiohttp.ClientSession() as session:         response = await session.get(\"https:\/\/www.example.com\")         return response.status   async def get_value(key: str):     async with lock:         if key not in cache:             print(f\"The value of key {key} is not in cache.\")             value = await request_remote()             cache[key] = value         else:             print(f\"The value of key {key} is already in cache.\")             value = cache[key]         print(f\"The value of {key} is {value}\")         return value   async def main():     task_one = asyncio.create_task(get_value(\"status\"))     task_two = asyncio.create_task(get_value(\"status\"))      await asyncio.gather(task_one, task_two)   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/94f\/40b\/645\/94f40b64575d62a2240c30410ca3956e.png\" alt=\"\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043a\u044d\u0448 \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" title=\"\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043a\u044d\u0448 \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" width=\"720\" height=\"154\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/94f\/40b\/645\/94f40b64575d62a2240c30410ca3956e.png\"\/><\/p>\n<div><figcaption>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043a\u044d\u0448 \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<h3>asyncio.Semaphore<\/h3>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u044f\u0442\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439. \u0418\u043b\u0438, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u0438\u043f\u0430 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438, \u0432\u0435\u0431-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-sync.html#semaphore\" rel=\"noopener noreferrer nofollow\">asyncio.Semaphore<\/a>. asyncio.Semaphore \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 Semaphore, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0435\u0442 \u043d\u0443\u043b\u044f.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0c2\/eb8\/114\/0c2eb8114bd790102f70a73c3bcd4bc8.png\" alt=\"\u0421\u0435\u043c\u0430\u0444\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" title=\"\u0421\u0435\u043c\u0430\u0444\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" width=\"720\" height=\"214\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0c2\/eb8\/114\/0c2eb8114bd790102f70a73c3bcd4bc8.png\"\/><\/p>\n<div><figcaption>\u0421\u0435\u043c\u0430\u0444\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0441\u0447\u0435\u0442\u0447\u0438\u043a asyncio.Semaphore \u0440\u0430\u0432\u0435\u043d \u043d\u0443\u043b\u044e, \u0434\u0440\u0443\u0433\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043d\u0443\u0436\u043d\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430, \u0431\u0443\u0434\u0443\u0442 \u0436\u0434\u0430\u0442\u044c. \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 release \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0431\u0443\u0434\u0435\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443. \u041e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import Semaphore from aiohttp import ClientSession   async def get_url(url: str, session: ClientSession, semaphore: Semaphore):     print('Waiting to acquire semaphore...')     async with semaphore:         print('Semaphore acquired, requesting...')         response = await session.get(url)         print('Finishing requesting')         return response.status   async def main():     # \u0425\u043e\u0442\u044f \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c 1000 \u0437\u0430\u0434\u0430\u0447, \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e 10 \u0437\u0430\u0434\u0430\u0447.     semaphore: Semaphore = Semaphore(10)     async with ClientSession() as session:         tasks = [asyncio.create_task(get_url(\"https:\/\/www.example.com\", session, semaphore))                  for _ in range(1000)]         await asyncio.gather(*tasks)   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439, \u043a \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f.<\/p>\n<h3>asyncio.BoundedSemaphore<\/h3>\n<p>\u041e\u0434\u043d\u0430 \u0438\u0437\u00a0\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u043e\u0432 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u00a0\u0442\u043e\u043c, \u0447\u0442\u043e\u00a0\u0447\u0438\u0441\u043b\u043e \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 release \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u044b\u0437\u043e\u0432\u043e\u0432 acquire. \u0415\u0441\u043b\u0438 \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u044b \u0432\u00a0\u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0438 \u0441\u00a0\u0431\u043b\u043e\u043a\u043e\u043c async with, \u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u0441\u00a0\u043a\u0430\u0436\u0434\u044b\u043c acquire \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438<br \/>\u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f release. \u041d\u043e\u00a0\u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043d\u0430\u0434 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u0437\u0430\u0445\u0432\u0430\u0442\u0430 \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f, \u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<p>\u0427\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <code>release()<\/code> \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437?<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import Semaphore   async def acquire(semaphore: Semaphore):     print(\"acquire: \u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u0430\")     async with semaphore:         print(\"acquire: \u0417\u0430\u0445\u0432\u0430\u0447\u0435\u043d...\")         await asyncio.sleep(5)     print(\"acquire: \u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f...\")   async def release(semaphore: Semaphore):     print(\"release: \u041e\u0434\u0438\u043d\u043e\u0447\u043d\u043e\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435...\")     semaphore.release()     print(\"release: \u041e\u0434\u0438\u043d\u043e\u0447\u043d\u043e\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 - \u0433\u043e\u0442\u043e\u0432\u043e!\")   async def main():     semaphore = Semaphore(2)     print(\"\u0414\u0432\u0430 \u0437\u0430\u0445\u0432\u0430\u0442\u0430, \u0442\u0440\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f...\")     await asyncio.gather(asyncio.create_task(acquire(semaphore)),                          asyncio.create_task(acquire(semaphore)),                          asyncio.create_task(release(semaphore)))     print(\"\u0422\u0440\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u0430...\")     await asyncio.gather(asyncio.create_task(acquire(semaphore)),                          asyncio.create_task(acquire(semaphore)),                          asyncio.create_task(acquire(semaphore)))   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0435\u043c\u0430\u0444\u043e\u0440 \u0441 \u043f\u0440\u0435\u0434\u0435\u043b\u043e\u043c 2. \u0417\u0430\u0442\u0435\u043c \u0434\u0432\u0430\u0436\u0434\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 acquire \u0438 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 release, \u0442.\u0435. \u0432\u0441\u0435\u0433\u043e \u0441\u0435\u043c\u0430\u0444\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0451\u043d \u0442\u0440\u0438\u0436\u0434\u044b. \u041f\u0435\u0440\u0432\u043e\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a gather \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043f\u043e \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/889\/a37\/1b0\/889a371b03e892449262559e41aab46e.png\" width=\"952\" height=\"261\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/889\/a37\/1b0\/889a371b03e892449262559e41aab46e.png\"\/><\/figure>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438 \u0432\u0442\u043e\u0440\u043e\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0441\u0435\u043c\u0430\u0444\u043e\u0440 \u0442\u0440\u0438 \u0440\u0430\u0437\u0430, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u2013 \u0432\u0441\u0435 \u0442\u0440\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442 \u0441\u0440\u0430\u0437\u0443! \u041c\u044b \u043d\u0435\u043f\u0440\u0435\u0434\u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u043b\u0438 \u043f\u0440\u0435\u0434\u0435\u043b \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a54\/e46\/625\/a54e466257f988c37c227a4deb2ed3bf.png\" width=\"845\" height=\"286\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a54\/e46\/625\/a54e466257f988c37c227a4deb2ed3bf.png\"\/><\/figure>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043a\u043e\u0434\u0430, \u043c\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u044b \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0434\u0432\u0443\u0445 \u0437\u0430\u0434\u0430\u0447, \u043d\u043e \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0432\u044b\u0437\u0432\u0430\u043b\u0438 release \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0430, \u0442\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0430\u0437 \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0442\u0440\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-sync.html#boundedsemaphore\" rel=\"noopener noreferrer nofollow\">asyncio.BoundedSemaphore<\/a>. Bounded Semaphore &#8212; \u044d\u0442\u043e \u0432\u0435\u0440\u0441\u0438\u044f Semaphore, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 ValueError \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 release(), \u0435\u0441\u043b\u0438  \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0448\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>\u041a\u0430\u043a \u043c\u044b \u0437\u043d\u0430\u0435\u043c \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 release \u0432\u044b\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430 ValueError, \u0435\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import BoundedSemaphore   async def main():     semaphore = BoundedSemaphore(2)      await semaphore.acquire()     semaphore.release()     semaphore.release()   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d7b\/821\/ace\/d7b821ace95034a868268f083b74acb1.png\" alt=\"\u041f\u0440\u0438 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 release \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 ValueError. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" title=\"\u041f\u0440\u0438 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 release \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 ValueError. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" width=\"720\" height=\"173\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d7b\/821\/ace\/d7b821ace95034a868268f083b74acb1.png\"\/><\/p>\n<div><figcaption>\u041f\u0440\u0438 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 release \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 ValueError. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432\u0442\u043e\u0440\u043e\u0439 \u0432\u044b\u0437\u043e\u0432 release \u0432\u043e\u0437\u0431\u0443\u0434\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 ValueError, \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0435\u0435, \u0447\u0442\u043e \u043c\u044b \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u043b\u0438 \u0441\u0435\u043c\u0430\u0444\u043e\u0440 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437. \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043c\u0435\u0441\u0442\u043e, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c BoundedSemaphore \u0432\u043c\u0435\u0441\u0442\u043e Semaphore. \u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 acquire \u0438 release \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u0442\u0430\u043a \u0447\u0442\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442\u044c<br \/>\u043f\u0440\u0435\u0434\u0435\u043b \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430, \u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 BoundedSemaphore, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0448\u0435\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0434\u0438\u0442 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435.<\/p>\n<h3>asyncio.Event<\/h3>\n<p>Event \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044e\u044e \u0431\u0443\u043b\u0435\u0432\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0444\u043b\u0430\u0433\u0430. <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-sync.html#event\" rel=\"noopener noreferrer nofollow\">asyncio.Event<\/a> \u0438\u043c\u0435\u0435\u0442 \u0442\u0440\u0438 \u043e\u0431\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u0430: wait, set \u0438 clear.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043e\u0431\u0435\u0433\u0430\u0435\u0442 \u0434\u043e event.wait(), \u043e\u043d\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f. \u0412 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c event.set(), \u0447\u0442\u043e\u0431\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u043c\u0430\u0440\u043a\u0435\u0440 \u0432 True, \u0438 \u0432\u0441\u0435 \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 event.clear(), \u0447\u0442\u043e\u0431\u044b \u0441\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0440\u043a\u0435\u0440\u0430 \u0432 False, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0430\u0437.<\/p>\n<p>\u0412 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u043c\u0435\u0441\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043a\u043e\u0434\u0430 \u044f \u043f\u043e\u043a\u0430\u0436\u0443, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Event \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439.<\/p>\n<h4>asyncio.Condition<\/h4>\n<p>asyncio.Event \u0445\u043e\u0440\u043e\u0448\u0438, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u0432\u0435\u0434\u043e\u043c\u0438\u0442\u044c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u043d\u0435\u0447\u0442\u043e, \u043d\u043e \u0431\u044b\u0432\u0430\u044e\u0442 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043f\u043e\u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0447\u0442\u043e \u043f\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u043c\u0443 \u0440\u0435\u0441\u0443\u0440\u0441\u0443, \u0442.\u0435. \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443. \u0418\u043b\u0438 \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u0434 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u0438\u0439, \u0447\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435. \u0418\u043b\u0438 \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0443\u0434\u0438\u0442\u044c \u043d\u0435 \u0432\u0441\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e. \u0412\u043e \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u0440\u0443\u0447\u0438\u0442\u044c <strong>asyncio.Condition<\/strong>.<\/p>\n<p>asyncio.Condition \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 asyncio.Lock \u0438 asyncio.Event \u0432\u043c\u0435\u0441\u0442\u0435 \u0432\u0437\u044f\u0442\u044b\u0435. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>async with<\/code>, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u044f, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>condition.wait()<\/code>, \u0447\u0442\u043e\u0431\u044b \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u0434\u043e\u0436\u0434\u0430\u0442\u044c.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 condition.wait() \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442, \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044f, \u0447\u0442\u043e\u0431\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u041f\u043e\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u043f\u043e \u043c\u0435\u0442\u043e\u0434\u0443 condition.wait(), \u0434\u0440\u0443\u0433\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043c\u043e\u0436\u0435\u0442 \u043b\u0438\u0431\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u0443\u0432\u0435\u0434\u043e\u043c\u0438\u0442\u044c \u0432\u0441\u0435 \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u043e \u043c\u0435\u0442\u043e\u0434\u0443 condition.notify_all().<\/p>\n<p>\u0411\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u043d\u0438\u0436\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e2b\/dc0\/3ef\/e2bdc03efdd852e5e25110f8a17da094.png\" alt=\"\u0420\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 asyncio.Condition. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" title=\"\u0420\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 asyncio.Condition. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" width=\"720\" height=\"382\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e2b\/dc0\/3ef\/e2bdc03efdd852e5e25110f8a17da094.png\"\/><\/p>\n<div><figcaption>\u0420\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 asyncio.Condition. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 asyncio.Condition \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import Condition   async def do_work(condition: Condition):     print(\"do_work: \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044f...\")     async with condition:         print(\"do_work: \u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0437\u0430\u0445\u0432\u0430\u0447\u0435\u043d\u0430, \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u044e \u0438 \u0436\u0434\u0443 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u0438\u044f...\")         await condition.wait()         print(\"do_work: \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043e, \u0432\u043d\u043e\u0432\u044c \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c...\")         await asyncio.sleep(1)         print(\"do_work: \u0420\u0430\u0431\u043e\u0442\u0430 \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u0430, \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0430.\")           async def fire_event(condition: Condition):     await asyncio.sleep(5)     print(\"fire_event: \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044f...\")     async with condition:         print(\"fire_event: \u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0437\u0430\u0445\u0432\u0430\u0447\u0435\u043d\u0430, \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u044f\u044e \u0432\u0441\u0435\u0445 \u0438\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u0435\u0439\")         condition.notify_all()         print(\"fire_event: \u0418\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u0438 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u044b, \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u044e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443.\")           async def main():     condition = Condition()     asyncio.create_task(fire_event(condition))      await asyncio.gather(do_work(condition), do_work(condition))   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b asyncio.Condition \u0436\u0434\u0430\u043b \u043d\u0430\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0448\u0430\u0433\u0443. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 condition.wait_for() \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0442\u043e\u0434. \u041f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 condition.notify_all \u043c\u0435\u0442\u043e\u0434 condition.wait_for \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u0430-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u043e\u043d \u0440\u0430\u0432\u0435\u043d True, \u0438\u043b\u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u043e\u043d \u0440\u0430\u0432\u0435\u043d False.<\/p>\n<p>condition.wait_for \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u0434\u0435 \u043c\u044b \u0441\u043c\u043e\u0434\u0435\u043b\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u0435\u0440\u0435\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c SQL-\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u043a\u043e\u0434 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u043b\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0437\u0430\u043f\u0440\u043e\u0441, \u0435\u0441\u043b\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430, \u0438\u043b\u0438 \u043f\u043e\u0434\u043e\u0436\u0434\u0435\u0442, \u043f\u043e\u043a\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import Condition from enum import Enum   class ConnectionState(Enum):     WAIT_INIT = 0     INITIALING = 1     INITIALIZED = 2   class Connection:     def __init__(self):         self._state = ConnectionState.WAIT_INIT         self._condition = Condition()      async def initialize(self):         print(\"initialize: Preparing initialize the connection.\")         await self._change_state(ConnectionState.INITIALING)         await asyncio.sleep(5)         print(\"initialize: Connection initialized\")         await self._change_state(ConnectionState.INITIALIZED)      async def execute(self, query: str):         async with self._condition:             print(\"execute: Waiting for connection initialized\")             await self._condition.wait_for(self._is_initialized)             print(f\"execute: Connection initialized, executing query: {query}\")             await asyncio.sleep(5)             print(\"execute: Execute finished.\")      async def _change_state(self, state: ConnectionState):         print(f\"_change_state: Will change state from {self._state} to {state}\")         self._state = state         print(\"_change_state: Change the state and notify all..\")         async with self._condition:             self._condition.notify_all()      def _is_initialized(self):         if self._state is not ConnectionState.INITIALIZED:             print(\"_is_initialized: The connection is not initialized.\")             return False         print(\"_is_initialized: The connection is ready.\")         return True   async def main():     connection = Connection()     task_one = asyncio.create_task(connection.execute(\"SELECT * FROM table\"))     task_two = asyncio.create_task(connection.execute(\"SELECT * FROM other_table\"))      asyncio.create_task(connection.initialize())     await asyncio.gather(task_one, task_two)   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<h2>asyncio.Barrier(\u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0432 Python 3.11)<\/h2>\n<p>\u041e\u0431\u044a\u0435\u043a\u0442 <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-sync.html#barrier\" rel=\"noopener noreferrer nofollow\">Barrier<\/a> &#8212; \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442. \u041d\u0435\u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0435\u043d. \u042d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0430 \u043d\u0435\u043c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447. \u0417\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0433\u0443\u0442 \u043e\u0436\u0438\u0434\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 wait() \u0438 \u0431\u0443\u0434\u0443\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u043d\u0435 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 wait(). \u0412 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0441\u0435 \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0440\u0430\u0437\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0442\u0441\u044f.<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c async with \u043a\u0430\u043a \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0443 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044e \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 Barrier.wait(). \u041a\u043b\u0430\u0441\u0441 asyncio.Barrier \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u043b\u044e\u0431\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437.<\/p>\n<pre><code class=\"python\">async def example_barrier():    # barrier with 3 parties    b = asyncio.Barrier(3)     # create 2 new waiting tasks    asyncio.create_task(b.wait())    asyncio.create_task(b.wait())     await asyncio.sleep(0)    print(b)     # The third .wait() call passes the barrier    await b.wait()    print(b)    print(\"barrier passed\")     await asyncio.sleep(0)    print(b)  asyncio.run(example_barrier())<\/code><\/pre>\n<p><span class=\"habrahidden\">\u0414\u0430, \u044d\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0431\u044b\u043b\u043e. \u041f\u043e \u044d\u0442\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443 \u043f\u043e\u043a\u0430 \u043d\u0435 \u043d\u0430\u0448\u0451\u043b \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432. \u0411\u0443\u0434\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043f\u043e \u043c\u0435\u0440\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438.<\/span><\/p>\n<h2>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0432\u0435\u0442\u044b \u043f\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 asyncio<\/h2>\n<h4>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u0438\u043b\u0438 \u043e\u0442\u043c\u0435\u043d\u0443 \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438<\/h4>\n<p>\u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b, \u043a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u0437-\u0437\u0430 \u0444\u043b\u0443\u043a\u0442\u0443\u0430\u0446\u0438\u0439 \u0432 \u0441\u0435\u0442\u0438 \u0438\u043b\u0438 \u043f\u043e \u0434\u0440\u0443\u0433\u0438\u043c \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430 \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0434\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0432 \u0434\u0440\u0443\u0433\u0438\u0445.\u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u0434\u043b\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0438 \u0434\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0430\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0435\u0435 \u0432\u043e\u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>\u0412 \u0434\u0440\u0443\u0433\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0436\u0434\u0430\u0442\u044c \u0432 \u0444\u043e\u043d\u043e\u0432\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u0438 \u043f\u043e\u043c\u0435\u0448\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439 cancel, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0440\u0432\u0430\u0442\u044c \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<h4>\u0418\u0437\u0431\u0435\u0433\u0430\u0439\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u043b\u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0435\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432<\/h4>\n<p>\u0412\u0441\u0435 \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e asyncio \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u0440\u0443\u0433\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 IO. \u041d\u043e \u0437\u0430\u0434\u0430\u0447\u0430 asyncio \u0447\u0430\u0441\u0442\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043a\u0430\u043a \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 IO, \u0442\u0430\u043a \u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043e\u043c. \u0415\u0441\u043b\u0438 \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u0442\u043e \u043e\u043d\u0430 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0432\u0440\u0435\u043c\u044f \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u0447\u0442\u043e \u0441\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u0435\u0441\u043b\u0438 \u0432 \u044d\u0442\u043e\u043c \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u0441\u0442\u0430\u0440\u0430\u0439\u0442\u0435\u0441\u044c \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u043b\u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432. <\/p>\n<h4>\u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439 \u0441 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0439 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u043e\u0439<\/h4>\n<p>\u0412 asyncio \u043d\u0435\u0442 <abbr class=\"habraabbr\" title=\"\u041a\u043b\u0430\u0441\u0441 RLock () \u043c\u043e\u0434\u0443\u043b\u044f threading \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0440\u0435\u0435\u043d\u0442\u0435\u0440\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0439 (\u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0439) \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0430\u044f\u0441\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0441\u043d\u044f\u0442\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b.\" data-title=\"&lt;p>\u041a\u043b\u0430\u0441\u0441&amp;nbsp;&lt;em>RLock&lt;\/em>() \u043c\u043e\u0434\u0443\u043b\u044f threading \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0440\u0435\u0435\u043d\u0442\u0435\u0440\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0439 (\u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0439) \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0430\u044f\u0441\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0441\u043d\u044f\u0442\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b.&lt;\/p>&#187; data-abbr=&#187;RLock&#187;>RLock<\/abbr>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0432 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u043c \u043a\u043e\u0434\u0435. \u041a\u0430\u043a \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e, \u0432 asyncio \u0442\u0430\u043a\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u044f \u0442\u0443\u043f\u0438\u043a\u043e\u0432\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0442\u0430\u0440\u0430\u0439\u0442\u0435\u0441\u044c \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a.<\/p>\n<h2>\u041f\u0440\u0438\u043c\u0435\u0440: \u0428\u0438\u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043d\u0430 \u0431\u0430\u0437\u0435 Asyncio<\/h2>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u043a \u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u043c\u0435\u043d\u0442\u0443 \u0432\u044b \u0441\u043c\u043e\u0433\u043b\u0438 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0438 \u043f\u043e\u043d\u044f\u0442\u044c \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 Asyncio. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0448\u0438\u043d\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u041a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u043e, \u043f\u0435\u0440\u0432\u044b\u043c \u0448\u0430\u0433\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 EventBus API.<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import Event import inspect from typing import Callable   class EventBus:     def __init__(self):         self._event_dict = dict()      async def on(self, event_name: str, fn: Callable):         PASS      def trigger(self, event_name: str, *args, **kwargs):         PASS      def _get_event(self, event_name: str):         PASS<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u044d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 EventBus \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0442\u0440\u043e\u043a, \u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u044f \u043d\u0430\u043c\u0435\u0440\u0435\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c asyncio.Event \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435, \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 _get_event:<\/p>\n<pre><code class=\"python\">def _get_event(self, event_name: str):         if event_name in self._event_dict:             print(\"event already inited...\")             event = self._event_dict.get(event_name)         else:             print(f\"need to init a new event for {event_name}\")             event = Event()             self._event_dict[event_name] = event         return event<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 on \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u0441\u043e\u0431\u044b\u0442\u0438\u044e:<\/p>\n<pre><code class=\"python\">async def on(self, event_name: str, fn: Callable):         event = self._get_event(event_name)         while True:             await event.wait()             print(\"event fired\")             result = fn(*event.args, **event.kwargs)             if inspect.isawaitable(result):                 await result                          # Since the callback function is likely a synchronous method,              # we must perform an await here to allow other tasks to execute.             await asyncio.sleep(0.1)             event.clear()<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 trigger \u043c\u043e\u0436\u0435\u0442 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"python\">def trigger(self, event_name: str, *args, **kwargs):     event = self._get_event(event_name)      event.args = args     event.kwargs = kwargs     event.set()<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043c\u0435\u0442\u043e\u0434 main, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 EventBus:<\/p>\n<pre><code class=\"python\">def a_sync_callback(data):     print(f\"A sync callback with data {data} is triggered\")   async def a_async_callback(data):     await asyncio.sleep(1)     print(f\"A async callback with data {data} is triggered\")   async def main():     event_bus = EventBus()     task_one = asyncio.create_task(event_bus.on(\"some_event\", a_async_callback))     task_two = asyncio.create_task(event_bus.on(\"some_event\", a_sync_callback))      event_bus.trigger(\"some_event\", {id: 1})     await asyncio.wait([task_one, task_two], timeout=20.0)   asyncio.run(main())   <\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0446\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 main \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u0439\u043c\u0430\u0443\u0442, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0430\u0441\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e, \u043a\u0430\u043a \u044f \u0443\u0436\u0435 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0430\u043b.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/00b\/1fa\/713\/00b1fa713269ad7c3bc63901b1efcd26.png\" alt=\"\u041a\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u043a\u0430\u043a \u0438 \u043e\u0436\u0438\u0434\u0430\u043b\u043e\u0441\u044c. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" title=\"\u041a\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u043a\u0430\u043a \u0438 \u043e\u0436\u0438\u0434\u0430\u043b\u043e\u0441\u044c. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.\" width=\"720\" height=\"204\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/00b\/1fa\/713\/00b1fa713269ad7c3bc63901b1efcd26.png\"\/><\/p>\n<div><figcaption>\u041a\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u043a\u0430\u043a \u0438 \u043e\u0436\u0438\u0434\u0430\u043b\u043e\u0441\u044c. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433 \u043a\u043e\u0434\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import asyncio from asyncio import Event import inspect from typing import Callable   class EventBus:     def __init__(self):         self._event_dict = dict()      def _get_event(self, event_name: str):         if event_name in self._event_dict:             print(\"event already inited...\")             event = self._event_dict.get(event_name)         else:             print(f\"need to init a new event for {event_name}\")             event = Event()             self._event_dict[event_name] = event         return event      async def on(self, event_name: str, fn: Callable):         event = self._get_event(event_name)         while True:             await event.wait()             print(\"event fired\")             result = fn(*event.args, **event.kwargs)             if inspect.isawaitable(result):                 await result              # \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c,             # \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0437\u0434\u0435\u0441\u044c await, \u0447\u0442\u043e\u0431\u044b \u0434\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0430\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c\u0441\u044f.             await asyncio.sleep(0.1)             event.clear()      def trigger(self, event_name: str, *args, **kwargs):         event = self._get_event(event_name)          event.args = args         event.kwargs = kwargs         event.set()   def a_sync_callback(data):     print(f\"A sync callback with data {data} is triggered\")   async def a_async_callback(data):     await asyncio.sleep(1)     print(f\"A async callback with data {data} is triggered\")   async def main():     event_bus = EventBus()     task_one = asyncio.create_task(event_bus.on(\"some_event\", a_async_callback))     task_two = asyncio.create_task(event_bus.on(\"some_event\", a_sync_callback))      event_bus.trigger(\"some_event\", {id: 1})     await asyncio.wait([task_one, task_two], timeout=20)  asyncio.run(main())<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<h2>\u0412\u044b\u0432\u043e\u0434\u044b<\/h2>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0431\u044b\u043b\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043d\u043e \u043e \u0442\u043e\u043c, \u0437\u0430\u0447\u0435\u043c Python asyncio \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438. \u0417\u0430\u0442\u0435\u043c \u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043b \u043b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u0434\u043b\u044f Lock, Semaphore, Event \u0438 Condition \u0438 \u0434\u0430\u043b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0432\u0435\u0442\u043e\u0432 \u043f\u043e \u0438\u0445 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e.<\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u044f \u043f\u0440\u0438\u0432\u0451\u043b \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 asyncio, \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u044f \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445.<\/p>\n<p>\u041d\u0435 \u0441\u0442\u0435\u0441\u043d\u044f\u0439\u0442\u0435\u0441\u044c \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u043e\u043f\u044b\u0442\u043e\u043c \u0438\u043b\u0438 \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u0442\u044c \u0441\u043e \u043c\u043d\u043e\u0439 \u0442\u0435\u043c\u044b, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 asyncio.<\/p>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u043d\u0442\u0430\u043a\u0442\u044b \u0430\u0432\u0442\u043e\u0440\u0430 \u0441\u0442\u0430\u0442\u044c\u0438<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430,\u00a0<a href=\"https:\/\/www.dataleadsfuture.com\/unleashing-the-power-of-python-asyncios-queue\/#\/portal\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0434\u043f\u0438\u0448\u0438\u0442\u0435\u0441\u044c<\/a>, \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0430\u0448\u043b\u0438 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c\u0438, \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0435 \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u043d\u0430 \u0441\u0432\u043e\u0439 \u043f\u043e\u0447\u0442\u043e\u0432\u044b\u0439 \u044f\u0449\u0438\u043a. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441\u044b, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u043c\u0435\u043d\u044f \u043d\u0430\u00a0<a href=\"https:\/\/www.linkedin.com\/in\/qtalen\/?ref=dataleadsfuture.com\" rel=\"noopener noreferrer nofollow\">LinkedIn<\/a>\u00a0\u0438\u043b\u0438 \u0432\u00a0<a href=\"https:\/\/twitter.com\/qtalen?ref=dataleadsfuture.com\" rel=\"noopener noreferrer nofollow\">Twitter(X)<\/a>.<\/p>\n<\/div>\n<\/details>\n<blockquote>\n<p>\u041f\u043e\u043c\u043e\u0433\u0430\u0439\u0442\u0435 \u0434\u0440\u0443\u0433\u0438\u043c \u0442\u0430\u043c, \u0433\u0434\u0435 \u0432\u044b \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u0435\u043b\u0430\u0442\u044c. <a href=\"https:\/\/habr.com\/ru\/docs\/help\/rules\/\" rel=\"noopener noreferrer nofollow\">(\u0441) \u0425\u0430\u0431\u0440\u0430\u044d\u0442\u0438\u043a\u0435\u0442<\/a><\/p>\n<\/blockquote>\n<\/div>\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\/767792\/\"> https:\/\/habr.com\/ru\/articles\/767792\/<\/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<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u00a0\u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0441\u00a0\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0438\u043b\u0438\u00a0\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c\u0438 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043e\u00a0\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0433\u043e\u043d\u043a\u0438 \u043f\u0440\u0438\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0435\u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u0414\u0430\u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043b\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u043d\u0430\u00a0\u0435\u0434\u0438\u043d\u0438\u0446\u0443 \u0432\u00a0\u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u0441\u00a0\u0442\u0440\u0443\u0434\u043e\u043c \u043f\u043e\u0434\u0434\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044e. \u041d\u043e\u00a0\u043f\u0440\u0438\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 asyncio \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0432\u00a0\u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 (\u0435\u0441\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u044f\u0432\u043d\u043e \u043d\u0435\u00a0\u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043b\u0438 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0438\u043b\u0438\u00a0\u043c\u043d\u043e\u0433\u043e\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438), \u0437\u043d\u0430\u0447\u0438\u0442, \u043c\u043e\u0436\u043d\u043e \u043d\u0435\u00a0\u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u044c\u0441\u044f \u043e\u00a0\u0433\u043e\u043d\u043a\u0430\u0445, \u043f\u0440\u0430\u0432\u0434\u0430? \u041d\u0430\u00a0\u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0441\u0435 \u043d\u0435\u00a0\u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e!<\/p>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438, \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0432\u00a0\u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0445 \u0438 \u043c\u043d\u043e\u0433\u043e\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445, \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u044b \u0432\u00a0\u0441\u0438\u043b\u0443 \u043e\u0434\u043d\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u043f\u0440\u0438\u0440\u043e\u0434\u044b asyncio, \u043d\u043e\u00a0\u044d\u0442\u043e \u043d\u0435\u00a0\u0441\u043e\u0432\u0441\u0435\u043c \u0442\u0430\u043a. \u0412\u0430\u043c \u0440\u0435\u0434\u043a\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0442\u044c \u043a\u00a0\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u00a0asyncio, \u043e\u0434\u043d\u0430\u043a\u043e \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0441\u00a0\u044d\u0442\u0438\u043c \u043d\u0443\u0436\u043d\u043e \u0447\u0442\u043e\u2011\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c. <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-sync.html\" rel=\"noopener noreferrer nofollow\"><strong>\u041f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 asyncio<\/strong><\/a> \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0434\u0435\u043b\u0438 \u043e\u0434\u043d\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0432\u044b \u0443\u0437\u043d\u0430\u0435\u0442\u0435, \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 asyncio, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043e \u043b\u0443\u0447\u0448\u0438\u0445 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438. \u0412 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438.<\/p>\n<details class=\"spoiler\">\n<summary>readme<\/summary>\n<div class=\"spoiler__content\">\n<p><em>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u0438\u043b\u0438 \u0432\u044b \u043d\u0430\u0448\u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0443, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 Ctrl+Enter \u0438 \u044f \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044e. \u0421\u043f\u0430\u0441\u0438\u0431\u043e!<\/em><\/p>\n<\/div>\n<\/details>\n<h2>\u0417\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 asyncio?<\/h2>\n<p>\u041b\u044e\u0431\u043e\u0439, \u043a\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0432 Python, \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0431\u043b\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u043d\u0435\u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u044c\u044e, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 asyncio \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a, \u043d\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043b\u0438 \u0443 \u043d\u0435\u0433\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c\u044e? \u041e\u0442\u0432\u0435\u0442: \u043d\u0435\u0442.<\/p>\n<p>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 asyncio \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u043e\u0434\u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430, \u0430 \u0434\u0440\u0443\u0433\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u044d\u0442\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043e\u0448\u0438\u0431\u043e\u043a, \u0432 Python asyncio \u0432\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438, \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438. \u042d\u0442\u043e <strong>asyncio.Lock, asyncio.Semaphore, asyncio.Event<\/strong> \u0438 <strong>asyncio.Condition<\/strong>.<strong> <\/strong>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u0437\u0430\u0434\u0430\u0447, \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 asyncio \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0437\u0430\u0449\u0438\u0442\u0438\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432 \u0447\u0438\u0441\u043b\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043a \u043d\u0435\u043c\u0443 \u0437\u0430\u0434\u0430\u0447. <\/p>\n<h3>\u041f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 asyncio<\/h3>\n<h3>asyncio.Lock<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0443\u0436\u043d\u0430 \u043a\u043e\u043f\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0430\u0439\u0442\u0430. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442, \u0435\u0441\u0442\u044c \u043b\u0438 \u043e\u043d\u0430 \u0432 \u043a\u044d\u0448\u0435, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0435\u0435 \u0438\u0437 \u043a\u044d\u0448\u0430, \u0430 \u0435\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u0442 \u0435\u0435 \u0441 \u0441\u0430\u0439\u0442\u0430. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0447\u0442\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0430\u0439\u0442\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u044d\u0448\u0430, \u043f\u0440\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432\u0441\u0435 \u043e\u043d\u0438 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0442, \u0447\u0442\u043e \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u044d\u0448\u0435 \u043d\u0435\u0442, \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u044e\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u0434\u0435:<\/p>\n<pre><code class=\"python\">import asyncio import aiohttp  cache = dict()   async def request_remote():     print(\"Will request the website to get status.\")     async with aiohttp.ClientSession() as session:         response = await session.get(\"https:\/\/www.example.com\")         return response.status   async def get_value(key: str):     if key not in cache:         print(f\"The value of key {key} is not in cache.\")         value = await request_remote()         cache[key] = value     else:         print(f\"The value of key {key} is already in cache.\")         value = cache[key]     print(f\"The value of {key} is {value}\")     return value   async def main():     task_one = asyncio.create_task(get_value(\"status\"))     task_two = asyncio.create_task(get_value(\"status\"))      await asyncio.gather(task_one, task_two)   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<figure class=\"full-width\">\n<div><figcaption>\u041e\u0431\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0447\u0438\u0442\u0430\u044e\u0442, \u0447\u0442\u043e \u0432 \u043a\u044d\u0448\u0435 \u043d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u043a \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u0441\u0430\u0439\u0442\u0443. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<p>\u042d\u0442\u043e \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0430\u0448\u0435\u043c\u0443 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0437\u0430\u043c\u044b\u0441\u043b\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-sync.html#asyncio.Lock\" rel=\"noopener noreferrer nofollow\"><strong>asyncio.Lock<\/strong><\/a>. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u044d\u0448\u0435, \u043a\u043e\u0433\u0434\u0430 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u0435\u0440\u0432\u044b\u043c\u0438, \u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443, \u0431\u0443\u0434\u0443\u0442 \u0436\u0434\u0430\u0442\u044c.<\/p>\n<p>\u041f\u043e\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0430, \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0430\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443, \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u044d\u0448\u0430 \u0438 \u043d\u0435 \u0441\u043d\u0438\u043c\u0435\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p>\u0412\u0441\u044f \u0431\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u043d\u0438\u0436\u0435:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0412\u0441\u044f \u0431\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 asyncio.Lock. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u043c \u043d\u0430\u0448 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import Lock  import aiohttp  cache = dict() lock = Lock()   async def request_remote():     print(\"Will request the website to get status.\")     async with aiohttp.ClientSession() as session:         response = await session.get(\"https:\/\/www.example.com\")         return response.status   async def get_value(key: str):     async with lock:         if key not in cache:             print(f\"The value of key {key} is not in cache.\")             value = await request_remote()             cache[key] = value         else:             print(f\"The value of key {key} is already in cache.\")             value = cache[key]         print(f\"The value of {key} is {value}\")         return value   async def main():     task_one = asyncio.create_task(get_value(\"status\"))     task_two = asyncio.create_task(get_value(\"status\"))      await asyncio.gather(task_one, task_two)   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<figure class=\"full-width\">\n<div><figcaption>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043a\u044d\u0448 \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<h3>asyncio.Semaphore<\/h3>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u044f\u0442\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439. \u0418\u043b\u0438, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u0438\u043f\u0430 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438, \u0432\u0435\u0431-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-sync.html#semaphore\" rel=\"noopener noreferrer nofollow\">asyncio.Semaphore<\/a>. asyncio.Semaphore \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 Semaphore, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0435\u0442 \u043d\u0443\u043b\u044f.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0421\u0435\u043c\u0430\u0444\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0441\u0447\u0435\u0442\u0447\u0438\u043a asyncio.Semaphore \u0440\u0430\u0432\u0435\u043d \u043d\u0443\u043b\u044e, \u0434\u0440\u0443\u0433\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043d\u0443\u0436\u043d\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430, \u0431\u0443\u0434\u0443\u0442 \u0436\u0434\u0430\u0442\u044c. \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 release \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0431\u0443\u0434\u0435\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443. \u041e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import Semaphore from aiohttp import ClientSession   async def get_url(url: str, session: ClientSession, semaphore: Semaphore):     print('Waiting to acquire semaphore...')     async with semaphore:         print('Semaphore acquired, requesting...')         response = await session.get(url)         print('Finishing requesting')         return response.status   async def main():     # \u0425\u043e\u0442\u044f \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c 1000 \u0437\u0430\u0434\u0430\u0447, \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e 10 \u0437\u0430\u0434\u0430\u0447.     semaphore: Semaphore = Semaphore(10)     async with ClientSession() as session:         tasks = [asyncio.create_task(get_url(\"https:\/\/www.example.com\", session, semaphore))                  for _ in range(1000)]         await asyncio.gather(*tasks)   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439, \u043a \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f.<\/p>\n<h3>asyncio.BoundedSemaphore<\/h3>\n<p>\u041e\u0434\u043d\u0430 \u0438\u0437\u00a0\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u043e\u0432 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u00a0\u0442\u043e\u043c, \u0447\u0442\u043e\u00a0\u0447\u0438\u0441\u043b\u043e \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 release \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u044b\u0437\u043e\u0432\u043e\u0432 acquire. \u0415\u0441\u043b\u0438 \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u044b \u0432\u00a0\u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0438 \u0441\u00a0\u0431\u043b\u043e\u043a\u043e\u043c async with, \u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u0441\u00a0\u043a\u0430\u0436\u0434\u044b\u043c acquire \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438<br \/>\u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f release. \u041d\u043e\u00a0\u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043d\u0430\u0434 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u0437\u0430\u0445\u0432\u0430\u0442\u0430 \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f, \u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<p>\u0427\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <code>release()<\/code> \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437?<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import Semaphore   async def acquire(semaphore: Semaphore):     print(\"acquire: \u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u0430\")     async with semaphore:         print(\"acquire: \u0417\u0430\u0445\u0432\u0430\u0447\u0435\u043d...\")         await asyncio.sleep(5)     print(\"acquire: \u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f...\")   async def release(semaphore: Semaphore):     print(\"release: \u041e\u0434\u0438\u043d\u043e\u0447\u043d\u043e\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435...\")     semaphore.release()     print(\"release: \u041e\u0434\u0438\u043d\u043e\u0447\u043d\u043e\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 - \u0433\u043e\u0442\u043e\u0432\u043e!\")   async def main():     semaphore = Semaphore(2)     print(\"\u0414\u0432\u0430 \u0437\u0430\u0445\u0432\u0430\u0442\u0430, \u0442\u0440\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f...\")     await asyncio.gather(asyncio.create_task(acquire(semaphore)),                          asyncio.create_task(acquire(semaphore)),                          asyncio.create_task(release(semaphore)))     print(\"\u0422\u0440\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u0430...\")     await asyncio.gather(asyncio.create_task(acquire(semaphore)),                          asyncio.create_task(acquire(semaphore)),                          asyncio.create_task(acquire(semaphore)))   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0435\u043c\u0430\u0444\u043e\u0440 \u0441 \u043f\u0440\u0435\u0434\u0435\u043b\u043e\u043c 2. \u0417\u0430\u0442\u0435\u043c \u0434\u0432\u0430\u0436\u0434\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 acquire \u0438 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 release, \u0442.\u0435. \u0432\u0441\u0435\u0433\u043e \u0441\u0435\u043c\u0430\u0444\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0451\u043d \u0442\u0440\u0438\u0436\u0434\u044b. \u041f\u0435\u0440\u0432\u043e\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a gather \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043f\u043e \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438 \u0432\u0442\u043e\u0440\u043e\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0441\u0435\u043c\u0430\u0444\u043e\u0440 \u0442\u0440\u0438 \u0440\u0430\u0437\u0430, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u2013 \u0432\u0441\u0435 \u0442\u0440\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442 \u0441\u0440\u0430\u0437\u0443! \u041c\u044b \u043d\u0435\u043f\u0440\u0435\u0434\u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u043b\u0438 \u043f\u0440\u0435\u0434\u0435\u043b \u0441\u0435\u043c\u0430\u0444\u043e\u0440\u0430:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043a\u043e\u0434\u0430, \u043c\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u044b \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0434\u0432\u0443\u0445 \u0437\u0430\u0434\u0430\u0447, \u043d\u043e \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0432\u044b\u0437\u0432\u0430\u043b\u0438 release \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0430, \u0442\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0430\u0437 \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0442\u0440\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio-sync.html#boundedsemaphore\" rel=\"noopener noreferrer nofollow\">asyncio.BoundedSemaphore<\/a>. Bounded Semaphore &#8212; \u044d\u0442\u043e \u0432\u0435\u0440\u0441\u0438\u044f Semaphore, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 ValueError \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 release(), \u0435\u0441\u043b\u0438  \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0448\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>\u041a\u0430\u043a \u043c\u044b \u0437\u043d\u0430\u0435\u043c \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 release \u0432\u044b\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430 ValueError, \u0435\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">import asyncio from asyncio import BoundedSemaphore   async def main():     semaphore = BoundedSemaphore(2)      await semaphore.acquire()     semaphore.release()     semaphore.release()   if __name__ == \"__main__\":     asyncio.run(main())<\/code><\/pre>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u0440\u0438 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 release \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 ValueError. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0430.<\/figcaption><\/div>\n<\/figure>\n<p>\u0417\u0434\u0435\u0441\u044c \u0432\u0442\u043e\u0440\u043e\u0439 \u0432\u044b\u0437\u043e\u0432 release<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-357611","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/357611","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=357611"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/357611\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=357611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=357611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}