{"id":443039,"date":"2024-12-26T04:22:39","date_gmt":"2024-12-26T04:22:39","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=443039"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-12-26T04:22:39","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=443039","title":{"rendered":"<span>\u041a\u0430\u043a \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0432 Aqueduct<\/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\u0432\u0435\u0442. \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0414\u0435\u043d\u0438\u0441 \u041b\u0438\u0441\u043e\u0432\u0438\u043a, \u044f <a href=\"https:\/\/career.avito.com\/vacancies\/?q=Backend&amp;action=filter\"><u>Backend-\u0438\u043d\u0436\u0435\u043d\u0435\u0440<\/u><\/a> \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 Data Science SWAT <a href=\"https:\/\/avito.tech\"><u>\u0410\u0432\u0438\u0442\u043e<\/u><\/a>. \u041d\u0430\u0448\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0432\u043e\u043a\u0440\u0443\u0433 \u0437\u0430\u0434\u0430\u0447 \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0414\u043b\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0443\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u0438 GPU-\u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0432\u043e\u044e \u043e\u043f\u0435\u043d\u0441\u043e\u0440\u0441-\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <a href=\"https:\/\/github.com\/avito-tech\/aqueduct\"><u>Aqueduct<\/u><\/a>. Aqueduct \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0442\u044f\u0436\u0435\u043b\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f event loop \u0438 \u0443\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u0434\u043b\u044f \u043d\u0430\u0441 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 Data Science\/ML \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432. \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e Aqueduct \u043c\u044b \u0443\u0436\u0435 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u0432\u043e\u0442 <a href=\"https:\/\/habr.com\/ru\/articles\/825908\/\"><u>\u0442\u0443\u0442<\/u><\/a>.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0432 Aqueduct \u0438 \u043a\u0430\u043a \u043f\u0440\u0438\u0448\u043b\u0438 \u043a \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044e. \u0412\u043c\u0435\u0441\u0442\u0435 \u0441 \u0432\u0430\u043c\u0438 \u043c\u044b \u0448\u0430\u0433 \u0437\u0430 \u0448\u0430\u0433\u043e\u043c \u043f\u0440\u043e\u0439\u0434\u0435\u043c \u043e\u0442 \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0434\u0432\u0430 \u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0434\u0438\u043d RPS, \u0434\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u043e\u0442\u043d\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443. \u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0432\u044b \u0443\u0437\u043d\u0430\u0435\u0442\u0435, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u043d\u0435 \u0443\u0442\u0435\u043a\u0430\u043b\u0430, \u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0435 \u043f\u0430\u0434\u0430\u043b\u043e \u0441 Segmentation fault.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/59f\/a20\/8b1\/59fa208b13f450c50b05de1c2da1275d.jpg\" width=\"780\" height=\"440\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/59f\/a20\/8b1\/59fa208b13f450c50b05de1c2da1275d.jpg\" data-blurred=\"true\"\/><\/figure>\n<h3>\u0427\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0442\u0430\u0442\u044c\u0438:<\/h3>\n<p><a href=\"#section1\">\u0421\u0435\u0440\u0432\u0438\u0441 \u0440\u0430\u0437\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/a><\/p>\n<p><a href=\"#section2\">Multiprocessing<\/a><\/p>\n<p><a href=\"#section3\">\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/a><\/p>\n<p><a href=\"#section4\">\u00ab\u0412\u043b\u0430\u0434\u0435\u043d\u0438\u0435\u00bb \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/a><\/p>\n<p><a href=\"#section5\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a<\/a><\/p>\n<p><a href=\"#section6\">\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a<\/a><\/p>\n<p><a href=\"#section7\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/a><\/p>\n<p><a class=\"anchor\" name=\"section1\" id=\"section1\"><\/a><\/p>\n<h3>\u0421\u0435\u0440\u0432\u0438\u0441 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/h3>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0436\u0438\u0432\u043e\u0442\u043d\u044b\u0445. \u041d\u0430 \u0432\u0445\u043e\u0434 \u0441\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0431\u0430\u0439\u0442\u044b \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0435\u043a\u0441\u0442 \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u0436\u0438\u0432\u043e\u0442\u043d\u043e\u0433\u043e.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9e4\/dc4\/b70\/9e4dc4b706d272261f8a8b997ef7b8c9.png\" alt=\"\u041e\u0431\u0449\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\" title=\"\u041e\u0431\u0449\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\" width=\"1600\" height=\"840\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9e4\/dc4\/b70\/9e4dc4b706d272261f8a8b997ef7b8c9.png\"\/><\/p>\n<div><figcaption>\u041e\u0431\u0449\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u041a\u043e\u043b\u043b\u0435\u0433\u0438 \u2013 Data Science-\u0438\u043d\u0436\u0435\u043d\u0435\u0440\u044b \u2013 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u043d\u0430\u043c \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438. \u041f\u0435\u0440\u0435\u0434 \u0432\u044b\u043a\u0430\u0442\u043a\u043e\u0439 \u0432 production \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043a\u0430\u043a\u0443\u044e \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441. \u041f\u0440\u043e\u0432\u043e\u0434\u0438\u043c \u043d\u0430\u0433\u0440\u0443\u0437\u043e\u0447\u043d\u043e\u0435 \u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e \u0441\u0435\u0440\u0432\u0438\u0441 \u0435\u0434\u0432\u0430 \u0434\u0435\u0440\u0436\u0438\u0442 1 RPS.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 CPU \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0442\u0435\u0441\u0442\u0430, \u0442\u043e \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043e\u0434\u043d\u043e \u044f\u0434\u0440\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430 100%, \u0430 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u043e\u0441\u0442\u0430\u0438\u0432\u0430\u044e\u0442. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0432\u0441\u0435 \u044f\u0434\u0440\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430, \u0447\u0442\u043e\u0431\u044b \u0443\u0442\u0438\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0443\u044e \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<p>\u041e\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437:<\/p>\n<ul>\n<li>\n<p>\u0433\u043b\u0430\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u2013 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 web-\u0441\u0435\u0440\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f;<\/p>\n<\/li>\n<li>\n<p>\u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0438 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u0435\u043c.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/770\/9b0\/d8b\/7709b0d8b890133bf436e256d26cb8af.png\" alt=\"\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\" title=\"\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\" width=\"1600\" height=\"1477\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/770\/9b0\/d8b\/7709b0d8b890133bf436e256d26cb8af.png\"\/><\/p>\n<div><figcaption>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u041c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u0434\u0430\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441: \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0435 \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u043e\u0432 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0438 \u043d\u0435 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043f\u0435\u0440\u0435\u0434 \u043d\u0438\u043c\u0438?<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/971\/62a\/b0c\/97162ab0c0f41b2dec7f31e0e532ef0d.png\" alt=\"\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0441 \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438\" title=\"\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0441 \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438\" width=\"1600\" height=\"1426\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/971\/62a\/b0c\/97162ab0c0f41b2dec7f31e0e532ef0d.png\"\/><\/p>\n<div><figcaption>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0441 \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u043e \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u044b \u2013 \u0432\u0441\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435.<\/p>\n<p><strong>\u041f\u0435\u0440\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/strong><\/p>\n<p>\u0423 \u043d\u0430\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u043d\u0441\u0442\u0430\u043d\u0441 \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0435\u0442 10 \u0413\u0431 \u0432\u0438\u0434\u0435\u043e-\u043f\u0430\u043c\u044f\u0442\u0438. \u0415\u0441\u043b\u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 \u0432 \u0431\u0430\u0442\u0447, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e \u0442\u0440\u0438 \u0448\u0442\u0443\u043a\u0438, \u0442\u043e \u0441\u0435\u0440\u0432\u0438\u0441 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435, \u0447\u0435\u043c \u0435\u0441\u043b\u0438 \u0431\u044b \u043e\u043d\u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u043c\u044b \u043f\u043e\u0434\u043d\u0438\u043c\u0435\u043c \u0442\u0440\u0438 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043a \u0442\u043e\u043c\u0443 \u0436\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b kubernetes \u043d\u0430 \u043d\u043e\u0434\u0443 \u0441 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u043e\u0439, \u0442\u043e \u043c\u044b \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u043c 30 \u0413\u0431 \u0432\u0438\u0434\u0435\u043e\u043f\u0430\u043c\u044f\u0442\u0438. \u0411\u0430\u0442\u0447 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u043f\u0435\u0432\u0430\u0442\u044c \u043d\u0430\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f, \u0445\u043e\u0442\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432 \u043e\u0431\u0449\u0435\u043c \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435, \u043a\u0430\u043a \u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043d\u043e \u0432 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0432\u0438\u0434\u0435\u043e\u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p><strong>\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/strong><\/p>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438 \u0447\u0430\u0441\u0442\u043e \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e CPU-bound \u044d\u0442\u0430\u043f\u043e\u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438: \u0440\u0435\u0441\u0430\u0439\u0437, \u043a\u0440\u043e\u043f, \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0432 grayscale, \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435.<\/p>\n<p>\u042d\u0442\u0438 \u044d\u0442\u0430\u043f\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0434\u043e\u043b\u0433\u0438\u043c\u0438 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0448\u0430\u0433\u043e\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043d\u0430 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0435\u0441\u043b\u0438 \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0438\u0445 \u0432 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u0445 \u043e\u0434\u043d\u0438\u043c \u0431\u0430\u0442\u0447\u043e\u043c.<\/p>\n<p>\u042d\u0442\u0438 \u044d\u0442\u0430\u043f\u044b \u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0440\u0430\u0437\u043d\u043e\u0441\u0438\u0442\u044c \u043f\u043e \u0440\u0430\u0437\u043d\u044b\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044d\u0442\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442 \u043e\u0431\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u043e\u0442\u0432\u0435\u0442\u0430, \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u0442 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0438 \u0434\u0430\u0441\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u0430 \u0441\u0435\u0442\u044c.<\/p>\n<p><a class=\"anchor\" name=\"section2\" id=\"section2\"><\/a><\/p>\n<h3>Multiprocessing<\/h3>\n<p>\u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u043d\u0430\u0448\u0435\u0439 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0438\u0434\u0435\u0435 \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f <code>ProcessPoolExecutor<\/code> \u0438\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 python.<\/p>\n<pre><code class=\"python\">from concurrent.futures import ProcessPoolExecutor  executor = ProcessPoolExecutor(max_workers=4)  def detect_image(image: bytes) -&gt; str:     # cpu-bound task     return 'cat'  async def http_handler(request: Request) -&gt; Response:     image = await request.read()     loop = asyncio.get_running_loop()     result = await loop.run_in_executor(executor, detect_image, image)     return Response(result)<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c5e\/08e\/f9f\/c5e08ef9fd5ce66d5e93cc73d09c3b5f.png\" alt=\"\u0421\u0435\u0440\u0432\u0438\u0441 \u0441 multiprocessing\" title=\"\u0421\u0435\u0440\u0432\u0438\u0441 \u0441 multiprocessing\" width=\"1600\" height=\"710\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c5e\/08e\/f9f\/c5e08ef9fd5ce66d5e93cc73d09c3b5f.png\"\/><\/p>\n<div><figcaption>\u0421\u0435\u0440\u0432\u0438\u0441 \u0441 multiprocessing<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0438 \u0432\u0441\u0435 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0417\u0430\u0434\u0430\u0447\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c \u044f\u0434\u0440\u0430\u043c. \u041f\u0440\u043e\u0432\u043e\u0434\u0438\u043c \u043d\u0430\u0433\u0440\u0443\u0437\u043e\u0447\u043d\u043e\u0435 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0445\u043e\u0440\u043e\u0448\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043f\u043e \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442\u0443 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435. \u041c\u044b \u0437\u0430\u043c\u0435\u0442\u0438\u043c \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u0443\u044e \u043f\u0440\u043e\u0441\u0430\u0434\u043a\u0443 \u0432 \u043d\u0430\u0448\u0435\u0439 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<p><strong>\u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442?<\/strong><\/p>\n<p>\u0412\u0441\u0435 \u0434\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>ProcessPoolExecutor<\/code> \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432, \u0438 \u044d\u0442\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u043a-\u0442\u043e \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u0438\u043b\u0438 \u0441 \u0433\u043b\u0430\u0432\u043d\u044b\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e <code>multiprocessing<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u044c <a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.html#multiprocessing.Queue\"><u>Queue<\/u><\/a>, \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.html#multiprocessing.Pipe\"><u>Pipe<\/u><\/a>. \u0427\u0435\u0440\u0435\u0437 Pipe \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c. \u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>pickle<\/code>, \u043f\u043e\u0442\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>unpickle<\/code>.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ffd\/651\/c7b\/ffd651c7b454ec2c73d69b6164d07f9a.png\" alt=\"pickle\/unpickle \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c\u0438\" title=\"pickle\/unpickle \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c\u0438\" width=\"1600\" height=\"729\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ffd\/651\/c7b\/ffd651c7b454ec2c73d69b6164d07f9a.png\"\/><\/p>\n<div><figcaption>pickle\/unpickle \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u0442\u0440\u0430\u0442\u0438\u043c \u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c\u0438.<\/p>\n<p><a class=\"anchor\" name=\"section3\" id=\"section3\"><\/a><\/p>\n<h3>\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/h3>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c (shared memory) \u2013 \u044d\u0442\u043e \u043e\u0431\u0449\u0438\u0439 \u0431\u043b\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438, \u043a \u043d\u0435\u043c\u0443 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043b\u044e\u0431\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u044d\u0442\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438\u043b\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043c\u043e\u0433\u0443\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u044d\u0442\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043e\u0431\u043c\u0435\u043d\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u0438.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/aa1\/258\/6a6\/aa12586a6bdffbd1534883d062b9465d.png\" alt=\"\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c\" title=\"\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c\" width=\"1600\" height=\"736\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/aa1\/258\/6a6\/aa12586a6bdffbd1534883d062b9465d.png\"\/><\/p>\n<div><figcaption>\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u0425\u043e\u0440\u043e\u0448\u0430\u044f \u043d\u043e\u0432\u043e\u0441\u0442\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e python \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435.<\/p>\n<pre><code class=\"python\">from concurrent.futures import ProcessPoolExecutor from multiprocessing import shared_memory  executor = ProcessPoolExecutor(max_workers=4)  def detect_image(shm: shared_memory.SharedMemory) -&gt; str:     # read image from shared memory     image = shm.buf     # cpu-bound task ...     return 'cat'  async def http_handler(request: Request) -&gt; Response:     loop = asyncio.get_running_loop()     image = await request.read()      # store image to shared memory     shm = shared_memory.SharedMemory(name='shared-mem-for-image', create=True, size=len(image))     shm.buf[:len(image)] = image     result = await loop.run_in_executor(executor, detect_image, shm)     return Response(result)<\/code><\/pre>\n<p>\u041d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430. \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0438\u00a0 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043e\u0431\u044a\u0435\u043a\u0442 <code>SharedMemory<\/code>, \u0443\u043a\u0430\u0437\u0430\u0432 \u0444\u043b\u0430\u0433 <code>create=True<\/code> \u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u0414\u0430\u043b\u0435\u0435 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0431\u0443\u0444\u0435\u0440 <code>SharedMemory<\/code> \u0431\u0430\u0439\u0442\u0430\u043c\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0438\u043d\u0441\u0442\u0430\u043d\u0441 <code>SharedMemory<\/code> \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u043f\u043e\u0434\u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0443\u0434\u0435\u0442 \u043d\u0435 \u0441\u0430\u043c\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043d\u0435\u0433\u043e, \u043f\u0440\u0438\u0447\u0435\u043c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u0435\u0440\u0432\u0438\u0441, \u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0443\u0442\u0435\u043a\u0430\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e Garbage Collector \u043d\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u0438 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u0430\u043c\u0438 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u043c <a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.shared_memory.html#multiprocessing.shared_memory.SharedMemory.close\"><u>\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/u><\/a> python \u0438 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<pre><code class=\"python\">...  def detect_image(shm: shared_memory.SharedMemory) -&gt; str:     image = shm.buf     # cpu-bound task ...     result = 'cat'     shm.close()     return result  async def http_handler(request: Request) -&gt; Response:     loop = asyncio.get_running_loop()     image = await request.read()      # store image to shared memory     shm = shared_memory.SharedMemory(name='shared-mem-for-image', create=True, size=len(image))     shm.buf[:len(image)] = image     result = await loop.run_in_executor(executor, detect_image, shm)     shm.close()     shm.unlink()     return Response(result)<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b <code>close()<\/code> \u0438 <code>unlink()<\/code>.<\/p>\n<ul>\n<li>\n<p>\u043c\u0435\u0442\u043e\u0434 <code>close()<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e;<\/p>\n<\/li>\n<li>\n<p>\u043c\u0435\u0442\u043e\u0434 <code>unlink()<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u0415\u0441\u043b\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0443\u0442\u0435\u0447\u0435\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435\u0442 \u0438 \u0441\u0435\u0440\u0432\u0438\u0441 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u043e.<\/p>\n<blockquote>\n<p>\u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432, \u043d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438. \u041f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u043e\u0434\u043e\u0432\u043e\u0439 \u0431\u0430\u0437\u0435 \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e-\u0442\u043e \u0433\u0434\u0435-\u0442\u043e \u043d\u0435 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u043b\u0438, \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043b\u0438\u0448\u043d\u0438\u0439 \u0440\u0430\u0437.<\/p>\n<\/blockquote>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0439\u0442\u0438 \u0438\u0441\u043a\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0442\u043e \u0432 <a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.shared_memory.html#multiprocessing.managers.SharedMemoryManager\"><u>\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/u><\/a> python \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043a\u043b\u0430\u0441\u0441 <code>SharedMemoryManager<\/code>.<\/p>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043e\u0447\u0438\u0449\u0430\u0442\u044c \u0435\u0435 \u0432 \u043d\u0443\u0436\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. \u0421\u0430\u043c \u043a\u043b\u0430\u0441\u0441 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b <code>start()<\/code> \u0438 <code>shutdown()<\/code> \u0432\u0440\u0443\u0447\u043d\u0443\u044e. \u041f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u043c \u043f\u0440\u0430\u0432\u0434\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u0430\u043c\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0441\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0432 \u043d\u0443\u0436\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u0435\u0442\u043e\u0434 <code>unlink()<\/code>.<\/p>\n<pre><code class=\"python\">from concurrent.futures import ProcessPoolExecutor from multiprocessing.managers import SharedMemoryManager  def detect_image(shm) -&gt; str:     image = shm.buf     # cpu-bound task ...     return 'cat'  async def http_handler(request: Request) -&gt; Response:     loop = asyncio.get_running_loop()     image = await request.read()      # store image to shared memory     with SharedMemoryManager() as manager:         shm = manager.SharedMemory(size=len(image))         shm.buf[:len(image)] = image         result = await loop.run_in_executor(executor, detect_image, shm)      return Response(result)<\/code><\/pre>\n<p>\u0423 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0438\u043d\u0443\u0441\u043e\u0432:<\/p>\n<ul>\n<li>\n<p><code>SharedMemoryManager<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0432\u0441\u0435 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c <code>Pipe<\/code> \u0438\u043b\u0438 \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u0441\u043e\u043a\u0435\u0442\u0430, \u0447\u0442\u043e \u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u043b <code>SharedMemoryManager<\/code>. \u0422\u043e \u0435\u0441\u0442\u044c \u0442\u0430\u043c \u0433\u0434\u0435 \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 <code>start()<\/code> \u0438\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0439 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0430\u043c\u043e\u043c\u0443 \u043d\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u043d\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442.<\/p>\n<p><a class=\"anchor\" name=\"section4\" id=\"section4\"><\/a><\/p>\n<h3>\u00ab\u0412\u043b\u0430\u0434\u0435\u043d\u0438\u0435\u00bb \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0438\u0434\u0435\u0435\u0439 \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u044f (ownership) \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u043c.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430.<\/p>\n<p>\u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c 2 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u2013 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0438 \u043f\u043e\u0434\u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a. \u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043e\u043d\u0438 \u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f. \u041f\u0440\u043e\u0446\u0435\u0441\u0441-\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443, \u0430 \u043e\u0442\u0434\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u043e\u0442\u0432\u0435\u0442\u043e\u043c.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0431\u043b\u043e\u043a \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c (\u0442\u043e \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441), \u0442\u043e \u043c\u044b \u0442\u0435\u0440\u044f\u0435\u043c \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u043d\u0430\u0434 \u044d\u0442\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e. \u0415\u0441\u043b\u0438 \u043c\u044b \u043d\u0435 \u0432\u043b\u0430\u0434\u0435\u0435\u043c \u044d\u0442\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0437\u043e\u0432\u0435\u0442\u0441\u044f \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>__del__<\/code>, \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u043c.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u043c\u044b \u0432\u043b\u0430\u0434\u0435\u0435\u043c \u044d\u0442\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>unlink()<\/code>, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043d\u0430\u043c, \u0447\u0442\u043e \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/39a\/0c0\/389\/39a0c03890e659a51e5008f726513dde.png\" alt=\"\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u00ab\u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435\u043c\u00bb \u0434\u0430\u043d\u043d\u044b\u043c\u0438\" title=\"\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u00ab\u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435\u043c\u00bb \u0434\u0430\u043d\u043d\u044b\u043c\u0438\" width=\"1600\" height=\"887\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/39a\/0c0\/389\/39a0c03890e659a51e5008f726513dde.png\"\/><\/p>\n<div><figcaption>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u00ab\u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435\u043c\u00bb \u0434\u0430\u043d\u043d\u044b\u043c\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441 <code>SharedMemoryWrapper<\/code><\/p>\n<pre><code class=\"python\">from multiprocessing import shared_memory  class SharedMemoryWrapper:     def __init__(self, data: bytes) -&gt; None:         self._shm = shared_memory.SharedMemory(name='shared-mem-for-image', create=True, size=len(data))         self._owner = True         self._shm.buf[:len(data)] = data      @property     def buf(self) -&gt; memoryview:         return self._shm.buf      def __getstate__(self) -&gt; dict[str, Any]:         self._owner = False         return {'name': self._shm.name}      def __setstate__(self, state: dict[str, Any]) -&gt; None:         self._shm = shared_memory.SharedMemory(name=state['name'], create=False)         self._owner = True      def __del__(self) -&gt; None:         self._shm.close()         if self._owner:             self._shm.unlink()<\/code><\/pre>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e \u0448\u0430\u0433\u0430\u043c:<\/p>\n<ol>\n<li>\n<p>\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0444\u043b\u0430\u0433 <code>_owner=True<\/code> \u0432 \u043d\u0430\u0448 wrapper;<\/p>\n<\/li>\n<li>\n<p>\u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432 <code>True<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0435\u0433\u043e \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u043c <code>_owner=False<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c \u0435\u0433\u043e \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c <code>_owner=True<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0435\u0441\u043b\u0438 GC \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0432 \u043f\u043e\u0434\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435, \u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>__del__<\/code>. \u0424\u043b\u0430\u0433 <code>_owner == True<\/code> &#8212; \u043c\u044b \u0432\u043b\u0430\u0434\u0435\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0435\u0433\u043e, \u0432\u044b\u0437\u0432\u0430\u0432 <code>unlink()<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0442\u0434\u0430\u043b \u043e\u0431\u044a\u0435\u043a\u0442 \u0438 \u043f\u043e\u0442\u0435\u0440\u044f\u043b \u043d\u0430\u0434 \u043d\u0438\u043c \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435. \u041e\u043d \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u044d\u0442\u043e\u0442 \u0436\u0435 \u0444\u043b\u0430\u0433 <code>_owner == False<\/code>, \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0442\u043e\u0434 <code>close()<\/code>.<\/p>\n<\/li>\n<\/ol>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043c\u0435\u0442\u043e\u0434\u044b <code>__getstate__<\/code> \u0438 <code>__setstate__<\/code>, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u0432\u044f\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u041f\u043e \u0444\u0430\u043a\u0442\u0443 \u044d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0438 <code>pickle<\/code> \u0438 <code>unpickle<\/code>.<\/p>\n<p><strong>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/strong><\/p>\n<pre><code class=\"python\">from concurrent.futures import ProcessPoolExecutor  def detect_image(shm: SharedMemoryWrapper) -&gt; str:     image = shm.buf     # cpu-bound task ...     return 'cat'  async def http_handler(request: Request) -&gt; Response:     loop = asyncio.get_running_loop()     image = await request.read()     shm = SharedMemoryWrapper(image)     result = await loop.run_in_executor(executor, detect_image, shm)     return Response(result)<\/code><\/pre>\n<p>\u041d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0438\u043b\u0438 \u0437\u0430\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 <code>SharedMemoryManager<\/code>. \u041a\u043e\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0435 \u0438 \u043e\u043d \u043f\u043e\u043d\u044f\u0442\u043d\u0435\u0435.<\/p>\n<p>\u0418 \u0432\u0441\u0435 \u0431\u044b\u043b\u043e \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435 \u0432\u043e \u0432\u0441\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445. \u042d\u0442\u043e\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/818\/1b5\/c24\/8181b5c24e1b9620bf57d13fd3502a60.png\" alt=\"\u041a\u043e\u043c\u0443 \u043e\u0447\u0438\u0449\u0430\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c\u00a0\u2014 \u043d\u0435\u00a0\u043f\u043e\u043d\u044f\u0442\u043d\u043e\" title=\"\u041a\u043e\u043c\u0443 \u043e\u0447\u0438\u0449\u0430\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c\u00a0\u2014 \u043d\u0435\u00a0\u043f\u043e\u043d\u044f\u0442\u043d\u043e\" width=\"1600\" height=\"685\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/818\/1b5\/c24\/8181b5c24e1b9620bf57d13fd3502a60.png\"\/><\/p>\n<div><figcaption>\u041a\u043e\u043c\u0443 \u043e\u0447\u0438\u0449\u0430\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c\u00a0\u2014 \u043d\u0435\u00a0\u043f\u043e\u043d\u044f\u0442\u043d\u043e<\/figcaption><\/div>\n<\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0441\u0432\u043e\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0436\u0438\u0432\u043e\u0442\u043d\u043e\u0435, \u043f\u043e\u0440\u043e\u0434\u0443 \u0438 \u0446\u0432\u0435\u0442, \u0442\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u043a\u0442\u043e \u0432\u043b\u0430\u0434\u0435\u0435\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0438 \u043a\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u043e\u0447\u0438\u0449\u0430\u0442\u044c \u0435\u0435.<\/p>\n<p>\u0414\u043b\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f \u043d\u0443\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a RC.<\/p>\n<p><a class=\"anchor\" name=\"section5\" id=\"section5\"><\/a><\/p>\n<h3>\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a<\/h3>\n<p>\u0412 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u043e \u0438 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c RC \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u0431\u0430\u0439\u0442\u0435 \u043d\u0430\u0448\u0435\u0439 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u041d\u0430\u0448 \u0444\u043b\u0430\u0433 <code>_owner<\/code> \u043c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u043d\u0430 \u0441\u0447\u0451\u0442\u0447\u0438\u043a \u0438 \u0431\u0443\u0434\u0435\u043c \u0435\u0433\u043e \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0434\u0435\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u041a\u043e\u0433\u0434\u0430 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d \u043d\u0443\u043b\u044e, \u043c\u044b \u0442\u043e\u0447\u043d\u043e \u0431\u0443\u0434\u0435\u043c \u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u043c \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<pre><code class=\"python\">from multiprocessing import shared_memory  class SharedMemoryWrapper:     def __init__(self, data: bytes) -&gt; None:         self._shm = shared_memory.SharedMemory(name='shared-mem-for-image', create=True, size=len(data) + 1)         self._shm.buf[:len(data)] = data         self._shm.buf[-1] = 1  # reference counter      @property     def buf(self) -&gt; memoryview:         return self._shm.buf      def __getstate__(self) -&gt; dict[str, Any]:         self._shm.buf[-1] += 1         return {'name': self._shm.name}      def __setstate__(self, state: dict[str, Any]) -&gt; None:         self._shm = shared_memory.SharedMemory(name=state['name'], create=False)      def __del__(self) -&gt; None:         self._shm.buf[-1] -= 1         rc = self._shm.buf[-1]         self._shm.close()         if rc == 0:             self._shm.unlink()<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u044d\u0442\u0443 \u0432\u0435\u0440\u0441\u0438\u044e, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c Segmentation Fault. \u041d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043b\u0438\u0431\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0430\u0434\u0430\u0442\u044c, \u043b\u0438\u0431\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u043d\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2a3\/e08\/3d8\/2a3e083d8045ac9c0e13e4f7638051bb.png\" alt=\"\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0443. \u0423\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438\" title=\"\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0443. \u0423\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438\" width=\"1600\" height=\"939\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2a3\/e08\/3d8\/2a3e083d8045ac9c0e13e4f7638051bb.png\"\/><\/p>\n<div><figcaption>\u041a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0443. \u0423\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u0417\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0433\u043e\u043d\u043a\u0430 (race condition). \u041a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0441\u0441\u044b\u043b\u043e\u043a, \u0430 \u043f\u043e\u0442\u043e\u043c \u0435\u0433\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0438\u043b\u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0434\u0435\u043b\u0430\u044e\u0442 \u0442\u043e\u0436\u0435 \u0441\u0430\u043c\u043e\u0435. \u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u043b\u0438\u0431\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0443\u043f\u0430\u0434\u0451\u0442, \u043b\u0438\u0431\u043e \u043d\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u043c \u0438 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0443\u0442\u0435\u0447\u043a\u0430.<\/p>\n<p>\u041b\u043e\u0433\u0438\u0447\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0441\u0441\u044b\u043b\u043e\u043a. \u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0432 \u0432\u0438\u0434\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0431\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443.<\/p>\n<pre><code class=\"python\">from multiprocessing import Manager, Lock  COUNTERS = Manager().dict()  class GlobalRC:     def __init__(self) -&gt; None:         self._counters = COUNTERS         self._lock = Lock()      def incr(self, shm_name: str) -&gt; None:         with self._lock:             if shm_name not in self._counters:                 self._counters[shm_name] = 0             self._counters[shm_name] += 1      def decr(self, shm_name: str) -&gt; None:         with self._lock:             self._counters[shm_name] -= 1             curr = self._counters[shm_name]             if curr == 0:                 self._counters.pop(shm_name)         return curr<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>Lock<\/code> \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f <code>multiprocessing<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443, \u0447\u0442\u043e\u0431\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0432 \u043b\u044e\u0431\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a.<\/p>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0431\u044b\u043b \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>Manager().dict()<\/code>. <code>Manager<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432\u0441\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c.<\/p>\n<p>\u0412\u0441\u0435 \u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043d\u0430\u043c \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0438 \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442.<\/p>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043e\u0447\u043d\u043e\u0435 \u0435\u0449\u0435 \u0440\u0430\u0437.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/eb3\/105\/b63\/eb3105b636d6aa3f8b6e2371a07212ee.png\" alt=\"\u0413\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0449\u0438\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0438 baseline \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a\" title=\"\u0413\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0449\u0438\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0438 baseline \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a\" width=\"1600\" height=\"853\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/eb3\/105\/b63\/eb3105b636d6aa3f8b6e2371a07212ee.png\"\/><\/p>\n<div><figcaption>\u0413\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0449\u0438\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0438 baseline \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0417\u0435\u043b\u0435\u043d\u044b\u0439 \u0433\u0440\u0430\u0444\u0438\u043a<\/strong> \u2013 \u044d\u0442\u043e baseline, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0431\u0449\u0438\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438, \u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043d\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0430.<\/p>\n<p><strong>\u041a\u0440\u0430\u0441\u043d\u044b\u0439 \u0433\u0440\u0430\u0444\u0438\u043a<\/strong> \u2013 \u044d\u0442\u043e \u0432\u0435\u0440\u0441\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0431\u0449\u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a.<\/p>\n<p>\u041f\u043e \u0433\u0440\u0430\u0444\u0438\u043a\u0443 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u043f\u043e\u0447\u0442\u0438 \u043a\u0430\u043a baseline. \u041f\u0440\u0438 \u0440\u043e\u0441\u0442\u0435 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432\u0435\u0440\u0441\u0438\u044f \u0441\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u043c \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0441\u0442\u0440\u0430\u0434\u0430\u0442\u044c, \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u043f\u043b\u0435\u0441\u043a\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0442\u0432\u0435\u0442\u0430, \u0438 \u0441\u0435\u0440\u0432\u0438\u0441 \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e.<\/p>\n<p>\u0412 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441 \u043e\u0431\u0449\u0438\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u043c \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435\u0435 \u043d\u0430 \u0441\u0432\u043e\u0439 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 GIL. \u0422\u0430\u043a\u0436\u0435 \u043d\u0430 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u043b\u0438\u044f\u0435\u0442, \u0447\u0442\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0447\u0435\u0440\u0435\u0437 <code>Manager<\/code> \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043d \u0436\u0438\u0432\u0435\u0442 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0438 \u0441 \u043d\u0438\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>socket<\/code> \u0438\u043b\u0438 <code>pipe<\/code> \u0441 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0438 \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439.<\/p>\n<p><a class=\"anchor\" name=\"section6\" id=\"section6\"><\/a><\/p>\n<h3>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a<\/h3>\n<p>\u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0432\u0435\u0441\u0442\u0438 \u0432\u0440\u0435\u043c\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a \u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0443 \u2014 \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0410\u0442\u043e\u043c\u0438\u043a\u0438. \u042d\u0442\u043e \u043d\u0430\u0431\u043e\u0440 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0445 CPU-\u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435, \u0433\u0434\u0435 \u0437\u0430 \u044d\u0442\u0443 \u043f\u0430\u043c\u044f\u0442\u044c \u043c\u043e\u0433\u0443\u0442 \u0431\u043e\u0440\u043e\u0442\u044c\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044f\u0434\u0435\u0440 CPU, \u044d\u043a\u0441\u043a\u043b\u044e\u0437\u0438\u0432\u043d\u043e \u0438 \u0431\u0435\u0437 \u0433\u043e\u043d\u043e\u043a.<\/p>\n<blockquote>\n<p>\u0422\u0443\u0442 \u043c\u044b \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u043c \u043f\u0435\u0440\u0432\u043e\u0435 \u043c\u0435\u0441\u0442\u043e, \u0433\u0434\u0435 \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 python \u043d\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b. \u0427\u0442\u043e\u0431\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0430\u0442\u043e\u043c\u0438\u043a\u0430\u043c\u0438, \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e C-\u043a\u043e\u0434\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <code>cffi<\/code>. \u042d\u0442\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043b\u0435\u0442\u0443 \u043f\u0440\u044f\u043c\u043e \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c C \u043a\u043e\u0434 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u0432 python-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435.<\/p>\n<\/blockquote>\n<p>\u0412\u0441\u0435\u0433\u043e \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0447\u0435\u0442\u044b\u0440\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438: \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<pre><code class=\"python\">atomic = ffi.verify(\"\"\" uint32_t load_uint32(uint32_t *v) {     return __atomic_load_n(v, __ATOMIC_SEQ_CST); }; void store_uint32(uint32_t *v, uint32_t n) {     uint32_t i = n;     __atomic_store(v, &amp;i, __ATOMIC_SEQ_CST); }; uint32_t add_and_fetch_uint32(uint32_t *v, uint32_t i) {     return __atomic_add_fetch(v, i, __ATOMIC_SEQ_CST); }; uint32_t sub_and_fetch_uint32(uint32_t *v, uint32_t i) {     return __atomic_sub_fetch(v, i, __ATOMIC_SEQ_CST); }; \"\"\") <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u044d\u0442\u0438 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u0432 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a <code>AtomicCounter<\/code><\/p>\n<pre><code class=\"python\">class AtomicCounter:     def __init__(self, view: memoryview):         self._ptr = ffi.cast('uint32_t*', ffi.from_buffer(view[:self.size()]))      def get(self):         return atomic.load_uint32(self._ptr)      def set(self, n):         return atomic.store_uint32(self._ptr, n)      def inc(self):         return atomic.add_and_fetch_uint32(self._ptr, 1)      def dec(self):         return atomic.sub_and_fetch_uint32(self._ptr, 1)      @staticmethod     def size():         return ffi.sizeof('uint32_t')<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0432 \u043d\u0430\u0448 <code>SharedMemoryWraper<\/code><\/p>\n<pre><code class=\"python\">class SharedMemoryWrapper:     def __init__(self, size: int, shm: shared_memory.SharedMemory = None, shm_name: str = None):         self._shm: shared_memory.SharedMemory = None  # noqa          if shm is None and shm_name is None:             rc_size = AtomicCounter.size()             shm = shared_memory.SharedMemory(create=True, size=size + rc_size)             self._rc = AtomicCounter(shm.buf)             self._rc.set(1)          self._attach_shm(shm, shm_name)         self._shm_name = self._shm.name      @property     def buf(self) -&gt; memoryview:         return self._shm.buf[AtomicCounter.size():]      @property     def ref_count(self) -&gt; int:         return self._rc.get()      def _attach_shm(self, shm: shared_memory.SharedMemory = None, shm_name: str = None):         if shm is None:             shm = shared_memory.SharedMemory(shm_name)         self._shm = shm         self._shm_name = shm.name      def __getstate__(self) -&gt; dict:         self._rc.inc()         return state      def __setstate__(self, state: dict):         self._attach_shm(shm_name=self._shm_name)         self._rc = AtomicCounter(self._shm.buf)      def __del__(self):         curr_rc = self._rc.dec()         if curr_rc == 0:             self._shm.unlink()<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0432\u043e\u0434\u0438\u043c \u043d\u0430\u0433\u0440\u0443\u0437\u043e\u0447\u043d\u043e\u0435<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/73a\/d88\/f8a\/73ad88f8a0c73cbd262d9a1ae79d683e.png\" width=\"1600\" height=\"934\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/73a\/d88\/f8a\/73ad88f8a0c73cbd262d9a1ae79d683e.png\"\/><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e \u0433\u0440\u0430\u0444\u0438\u043a\u0443 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0433\u0440\u0430\u0444\u0438\u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0433\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0441 baseline \u0433\u0440\u0430\u0444\u0438\u043a\u043e\u043c \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u0442 \u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b\u0445 \u0441\u043a\u0430\u0447\u043a\u043e\u0432 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0442\u0432\u0435\u0442\u0430.<\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0443 \u0441\u0435\u0431\u044f \u0432 Aqueduct \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e.<\/p>\n<p>\u0423 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0435\u0441\u0442\u044c \u0438 \u043c\u0438\u043d\u0443\u0441. \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u043c\u043e\u0433\u0443\u0442 \u0443\u043f\u0430\u0441\u0442\u044c \u0438\u0437-\u0437\u0430 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043d\u0435\u043f\u0440\u0435\u0434\u0432\u0438\u0434\u0435\u043d\u043d\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438\u043b\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u0445 \u0443\u0431\u0438\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u043d\u0438 \u043d\u0435 \u0441\u043c\u043e\u0433\u0443\u0442 \u0434\u0435\u043a\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0434\u0430\u043b\u044c\u0448\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u043f\u0430\u0432\u0448\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0442\u043e \u0443 \u043d\u0430\u0441 \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u043b\u0438 segmentation fault \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u043b\u0438\u0448\u043d\u0438\u0439 \u0440\u0430\u0437 \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<p>\u0423 \u0441\u0435\u0431\u044f \u0432 Aqueduct \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438, \u0447\u0442\u043e \u043f\u0440\u0438 \u043f\u0430\u0434\u0435\u043d\u0438\u0438 \u043b\u044e\u0431\u043e\u0433\u043e \u043f\u043e\u0434\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u043e\u0434\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u0438 \u0440\u0430\u0431\u043e\u0442\u0443 \u0432\u0441\u0435\u0433\u043e \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u0430. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0448 \u043c\u0435\u0442\u043e\u0434 <code>is_running<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>False<\/code> \u0438 \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043d\u043e\u0432\u044b\u0435 \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043c\u044b \u0431\u0440\u043e\u0441\u0430\u0435\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <code>NotRunningError<\/code>.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u0447\u0442\u043e Aqueduct \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u0430 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 (kubernetes, systemd, supervisord) \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435.<\/p>\n<p><a class=\"anchor\" name=\"section7\" id=\"section7\"><\/a><\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432 python. \u041a\u0430\u0436\u0434\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c \u0432 \u0442\u043e\u0439 \u0438\u043b\u0438 \u0438\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435.<\/p>\n<p>\u041c\u044b \u043d\u0430\u0447\u0430\u043b\u0438 \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u0432 \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a \u043f\u0440\u043e\u0433\u043e\u043d\u044f\u043b \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u0438 \u0435\u0434\u0432\u0430 \u0434\u0435\u0440\u0436\u0430\u043b 1 RPS. \u0414\u043e\u0431\u0430\u0432\u0438\u0432 \u0442\u0440\u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430, \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0438, \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0440\u0430\u0437\u043e\u0433\u043d\u0430\u0442\u044c \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043e 900 RPS.<\/p>\n<p>\u0412\u0441\u0435 \u044d\u0442\u043e \u043c\u044b \u0434\u0435\u043b\u0430\u043b\u0438 \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 python. \u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0442\u0430\u043a\u0438\u0435 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u044b \u043f\u0438\u0441\u0430\u0442\u044c \u0434\u043e\u043b\u0433\u043e \u0438 \u0438\u0445 \u0441\u043b\u043e\u0436\u043d\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0415\u0449\u0435 \u0438 \u0432\u0441\u0435 \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u0448\u0430\u0433\u043e\u0432 \u0432 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u0434\u043d\u043e\u0433\u043e.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/github.com\/avito-tech\/aqueduct\/\">Aqueduct<\/a>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u0432\u044b\u0448\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438.<\/p>\n<p>\u0415\u0441\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u043b\u0438\u044f\u044e\u0442 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\u0430:<\/p>\n<ul>\n<li>\n<p>\u0411\u0430\u0442\u0447\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 &#8212; \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0443\u0442\u0438\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 GPU, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u0432\u0438\u0434\u0435\u043e \u043a\u0430\u0440\u0442\u0443 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0434\u043d\u043e\u0439 \u043f\u0430\u0447\u043a\u043e\u0439, \u0430 \u043d\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0434\u043d\u043e \u0437\u0430 \u0434\u0440\u0443\u0433\u0438\u043c<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c &#8212; \u043c\u0435\u043d\u044c\u0448\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u0438 \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>Aqueduct \u2013 \u044d\u0442\u043e \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u0440\u043e\u0446\u0435\u0441c\u0438\u043d\u0433\u043e\u0432\u044b\u0445 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u043e\u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442:<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u043e\u0441\u0442\u044c \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430 \u0432 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u043f\u0440\u043e\u0446\u0435\u0441\u0430\u0445 &#8212; \u0448\u0430\u0433\u0430\u0445 (<code>FlowStep<\/code>)<\/p>\n<\/li>\n<li>\n<p>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 (<code>FLow<\/code>)<\/p>\n<\/li>\n<li>\n<p>\u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u0440\u043e\u0446\u0435\u0441c\u0438\u043d\u0433\u043e\u043c &#8212; \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0434\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0448\u0430\u0433 \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u0430 \u0438 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u0445 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u0421\u0431\u043e\u0440 \u043c\u0435\u0442\u0440\u0438\u043a \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0430\u0439\u043f\u043b\u0430\u0439\u043d\u0430.<\/p>\n<\/li>\n<\/ul>\n<p>\u041a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0443 \u043d\u0430\u0441 \u0432 Aqueduct <a href=\"https:\/\/github.com\/avito-tech\/aqueduct\/blob\/main\/aqueduct\/shm\/base.py\">\u0442\u0443\u0442<\/a>.<\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441\u044b \u0438 \u043e\u0442\u0437\u044b\u0432\u044b \u043e Aqueduct <strong>\u043e\u0447\u0435\u043d\u044c \u0436\u0434\u0435\u043c \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043a \u0441\u0442\u0430\u0442\u044c\u0435!<\/strong> \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0432\u0430\u043c \u0437\u0430 \u0443\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u043d\u0430\u0448 \u043e\u043f\u044b\u0442 \u0438 \u043d\u0430\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u0430\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u044b.<\/p>\n<\/p>\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\/866338\/\"> https:\/\/habr.com\/ru\/articles\/866338\/<\/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\u0432\u0435\u0442. \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0414\u0435\u043d\u0438\u0441 \u041b\u0438\u0441\u043e\u0432\u0438\u043a, \u044f <a href=\"https:\/\/career.avito.com\/vacancies\/?q=Backend&amp;action=filter\"><u>Backend-\u0438\u043d\u0436\u0435\u043d\u0435\u0440<\/u><\/a> \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 Data Science SWAT <a href=\"https:\/\/avito.tech\"><u>\u0410\u0432\u0438\u0442\u043e<\/u><\/a>. \u041d\u0430\u0448\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0432\u043e\u043a\u0440\u0443\u0433 \u0437\u0430\u0434\u0430\u0447 \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0414\u043b\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0443\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u0438 GPU-\u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0432\u043e\u044e \u043e\u043f\u0435\u043d\u0441\u043e\u0440\u0441-\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <a href=\"https:\/\/github.com\/avito-tech\/aqueduct\"><u>Aqueduct<\/u><\/a>. Aqueduct \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0442\u044f\u0436\u0435\u043b\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f event loop \u0438 \u0443\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u0434\u043b\u044f \u043d\u0430\u0441 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 Data Science\/ML \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432. \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e Aqueduct \u043c\u044b \u0443\u0436\u0435 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u0432\u043e\u0442 <a href=\"https:\/\/habr.com\/ru\/articles\/825908\/\"><u>\u0442\u0443\u0442<\/u><\/a>.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0432 Aqueduct \u0438 \u043a\u0430\u043a \u043f\u0440\u0438\u0448\u043b\u0438 \u043a \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044e. \u0412\u043c\u0435\u0441\u0442\u0435 \u0441 \u0432\u0430\u043c\u0438 \u043c\u044b \u0448\u0430\u0433 \u0437\u0430 \u0448\u0430\u0433\u043e\u043c \u043f\u0440\u043e\u0439\u0434\u0435\u043c \u043e\u0442 \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0434\u0432\u0430 \u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0434\u0438\u043d RPS, \u0434\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u043e\u0442\u043d\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443. \u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0432\u044b \u0443\u0437\u043d\u0430\u0435\u0442\u0435, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u043d\u0435 \u0443\u0442\u0435\u043a\u0430\u043b\u0430, \u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0435 \u043f\u0430\u0434\u0430\u043b\u043e \u0441 Segmentation fault.<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>\u0427\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0442\u0430\u0442\u044c\u0438:<\/h3>\n<p><a href=\"#section1\">\u0421\u0435\u0440\u0432\u0438\u0441 \u0440\u0430\u0437\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/a><\/p>\n<p><a href=\"#section2\">Multiprocessing<\/a><\/p>\n<p><a href=\"#section3\">\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/a><\/p>\n<p><a href=\"#section4\">\u00ab\u0412\u043b\u0430\u0434\u0435\u043d\u0438\u0435\u00bb \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/a><\/p>\n<p><a href=\"#section5\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a<\/a><\/p>\n<p><a href=\"#section6\">\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a<\/a><\/p>\n<p><a href=\"#section7\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/a><\/p>\n<p><a class=\"anchor\" name=\"section1\" id=\"section1\"><\/a><\/p>\n<h3>\u0421\u0435\u0440\u0432\u0438\u0441 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/h3>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0436\u0438\u0432\u043e\u0442\u043d\u044b\u0445. \u041d\u0430 \u0432\u0445\u043e\u0434 \u0441\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0431\u0430\u0439\u0442\u044b \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0435\u043a\u0441\u0442 \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u0436\u0438\u0432\u043e\u0442\u043d\u043e\u0433\u043e.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041e\u0431\u0449\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u041a\u043e\u043b\u043b\u0435\u0433\u0438 \u2013 Data Science-\u0438\u043d\u0436\u0435\u043d\u0435\u0440\u044b \u2013 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u043d\u0430\u043c \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438. \u041f\u0435\u0440\u0435\u0434 \u0432\u044b\u043a\u0430\u0442\u043a\u043e\u0439 \u0432 production \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043a\u0430\u043a\u0443\u044e \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441. \u041f\u0440\u043e\u0432\u043e\u0434\u0438\u043c \u043d\u0430\u0433\u0440\u0443\u0437\u043e\u0447\u043d\u043e\u0435 \u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e \u0441\u0435\u0440\u0432\u0438\u0441 \u0435\u0434\u0432\u0430 \u0434\u0435\u0440\u0436\u0438\u0442 1 RPS.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 CPU \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0442\u0435\u0441\u0442\u0430, \u0442\u043e \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043e\u0434\u043d\u043e \u044f\u0434\u0440\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430 100%, \u0430 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u043e\u0441\u0442\u0430\u0438\u0432\u0430\u044e\u0442. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0432\u0441\u0435 \u044f\u0434\u0440\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430, \u0447\u0442\u043e\u0431\u044b \u0443\u0442\u0438\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0443\u044e \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<p>\u041e\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437:<\/p>\n<ul>\n<li>\n<p>\u0433\u043b\u0430\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u2013 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 web-\u0441\u0435\u0440\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f;<\/p>\n<\/li>\n<li>\n<p>\u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0438 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u0435\u043c.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\">\n<div><figcaption>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u041c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u0434\u0430\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441: \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0435 \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u043e\u0432 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0438 \u043d\u0435 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u043f\u0435\u0440\u0435\u0434 \u043d\u0438\u043c\u0438?<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0441 \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u043e\u043c \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u043e \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u044b \u2013 \u0432\u0441\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435.<\/p>\n<p><strong>\u041f\u0435\u0440\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/strong><\/p>\n<p>\u0423 \u043d\u0430\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u043d\u0441\u0442\u0430\u043d\u0441 \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0435\u0442 10 \u0413\u0431 \u0432\u0438\u0434\u0435\u043e-\u043f\u0430\u043c\u044f\u0442\u0438. \u0415\u0441\u043b\u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 \u0432 \u0431\u0430\u0442\u0447, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e \u0442\u0440\u0438 \u0448\u0442\u0443\u043a\u0438, \u0442\u043e \u0441\u0435\u0440\u0432\u0438\u0441 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435, \u0447\u0435\u043c \u0435\u0441\u043b\u0438 \u0431\u044b \u043e\u043d\u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u043c\u044b \u043f\u043e\u0434\u043d\u0438\u043c\u0435\u043c \u0442\u0440\u0438 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043a \u0442\u043e\u043c\u0443 \u0436\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b kubernetes \u043d\u0430 \u043d\u043e\u0434\u0443 \u0441 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u043e\u0439, \u0442\u043e \u043c\u044b \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u043c 30 \u0413\u0431 \u0432\u0438\u0434\u0435\u043e\u043f\u0430\u043c\u044f\u0442\u0438. \u0411\u0430\u0442\u0447 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u043f\u0435\u0432\u0430\u0442\u044c \u043d\u0430\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f, \u0445\u043e\u0442\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432 \u043e\u0431\u0449\u0435\u043c \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435, \u043a\u0430\u043a \u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043d\u043e \u0432 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0432\u0438\u0434\u0435\u043e\u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p><strong>\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/strong><\/p>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438 \u0447\u0430\u0441\u0442\u043e \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e CPU-bound \u044d\u0442\u0430\u043f\u043e\u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438: \u0440\u0435\u0441\u0430\u0439\u0437, \u043a\u0440\u043e\u043f, \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0432 grayscale, \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435.<\/p>\n<p>\u042d\u0442\u0438 \u044d\u0442\u0430\u043f\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0434\u043e\u043b\u0433\u0438\u043c\u0438 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0448\u0430\u0433\u043e\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043d\u0430 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0435\u0441\u043b\u0438 \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0438\u0445 \u0432 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u0445 \u043e\u0434\u043d\u0438\u043c \u0431\u0430\u0442\u0447\u043e\u043c.<\/p>\n<p>\u042d\u0442\u0438 \u044d\u0442\u0430\u043f\u044b \u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0440\u0430\u0437\u043d\u043e\u0441\u0438\u0442\u044c \u043f\u043e \u0440\u0430\u0437\u043d\u044b\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044d\u0442\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442 \u043e\u0431\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u043e\u0442\u0432\u0435\u0442\u0430, \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u0442 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0438 \u0434\u0430\u0441\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u0430 \u0441\u0435\u0442\u044c.<\/p>\n<p><a class=\"anchor\" name=\"section2\" id=\"section2\"><\/a><\/p>\n<h3>Multiprocessing<\/h3>\n<p>\u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u043d\u0430\u0448\u0435\u0439 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0438\u0434\u0435\u0435 \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f <code>ProcessPoolExecutor<\/code> \u0438\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 python.<\/p>\n<pre><code class=\"python\">from concurrent.futures import ProcessPoolExecutor  executor = ProcessPoolExecutor(max_workers=4)  def detect_image(image: bytes) -&gt; str:     # cpu-bound task     return 'cat'  async def http_handler(request: Request) -&gt; Response:     image = await request.read()     loop = asyncio.get_running_loop()     result = await loop.run_in_executor(executor, detect_image, image)     return Response(result)<\/code><\/pre>\n<figure class=\"full-width\">\n<div><figcaption>\u0421\u0435\u0440\u0432\u0438\u0441 \u0441 multiprocessing<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0438 \u0432\u0441\u0435 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0417\u0430\u0434\u0430\u0447\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c \u044f\u0434\u0440\u0430\u043c. \u041f\u0440\u043e\u0432\u043e\u0434\u0438\u043c \u043d\u0430\u0433\u0440\u0443\u0437\u043e\u0447\u043d\u043e\u0435 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0445\u043e\u0440\u043e\u0448\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043f\u043e \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442\u0443 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435. \u041c\u044b \u0437\u0430\u043c\u0435\u0442\u0438\u043c \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u0443\u044e \u043f\u0440\u043e\u0441\u0430\u0434\u043a\u0443 \u0432 \u043d\u0430\u0448\u0435\u0439 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<p><strong>\u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442?<\/strong><\/p>\n<p>\u0412\u0441\u0435 \u0434\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>ProcessPoolExecutor<\/code> \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432, \u0438 \u044d\u0442\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u043a-\u0442\u043e \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u0438\u043b\u0438 \u0441 \u0433\u043b\u0430\u0432\u043d\u044b\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e <code>multiprocessing<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u044c <a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.html#multiprocessing.Queue\"><u>Queue<\/u><\/a>, \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.html#multiprocessing.Pipe\"><u>Pipe<\/u><\/a>. \u0427\u0435\u0440\u0435\u0437 Pipe \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c. \u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>pickle<\/code>, \u043f\u043e\u0442\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>unpickle<\/code>.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>pickle\/unpickle \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u0442\u0440\u0430\u0442\u0438\u043c \u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c\u0438.<\/p>\n<p><a class=\"anchor\" name=\"section3\" id=\"section3\"><\/a><\/p>\n<h3>\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/h3>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c (shared memory) \u2013 \u044d\u0442\u043e \u043e\u0431\u0449\u0438\u0439 \u0431\u043b\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438, \u043a \u043d\u0435\u043c\u0443 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043b\u044e\u0431\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u044d\u0442\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438\u043b\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043c\u043e\u0433\u0443\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u044d\u0442\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043e\u0431\u043c\u0435\u043d\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u0438.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u0425\u043e\u0440\u043e\u0448\u0430\u044f \u043d\u043e\u0432\u043e\u0441\u0442\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e python \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435.<\/p>\n<pre><code class=\"python\">from concurrent.futures import ProcessPoolExecutor from multiprocessing import shared_memory  executor = ProcessPoolExecutor(max_workers=4)  def detect_image(shm: shared_memory.SharedMemory) -&gt; str:     # read image from shared memory     image = shm.buf     # cpu-bound task ...     return 'cat'  async def http_handler(request: Request) -&gt; Response:     loop = asyncio.get_running_loop()     image = await request.read()      # store image to shared memory     shm = shared_memory.SharedMemory(name='shared-mem-for-image', create=True, size=len(image))     shm.buf[:len(image)] = image     result = await loop.run_in_executor(executor, detect_image, shm)     return Response(result)<\/code><\/pre>\n<p>\u041d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430. \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0438\u00a0 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043e\u0431\u044a\u0435\u043a\u0442 <code>SharedMemory<\/code>, \u0443\u043a\u0430\u0437\u0430\u0432 \u0444\u043b\u0430\u0433 <code>create=True<\/code> \u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u0414\u0430\u043b\u0435\u0435 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0431\u0443\u0444\u0435\u0440 <code>SharedMemory<\/code> \u0431\u0430\u0439\u0442\u0430\u043c\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0438\u043d\u0441\u0442\u0430\u043d\u0441 <code>SharedMemory<\/code> \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u043f\u043e\u0434\u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0443\u0434\u0435\u0442 \u043d\u0435 \u0441\u0430\u043c\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043d\u0435\u0433\u043e, \u043f\u0440\u0438\u0447\u0435\u043c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u0435\u0440\u0432\u0438\u0441, \u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0443\u0442\u0435\u043a\u0430\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e Garbage Collector \u043d\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u0438 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u0430\u043c\u0438 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u043c <a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.shared_memory.html#multiprocessing.shared_memory.SharedMemory.close\"><u>\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/u><\/a> python \u0438 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<pre><code class=\"python\">...  def detect_image(shm: shared_memory.SharedMemory) -&gt; str:     image = shm.buf     # cpu-bound task ...     result = 'cat'     shm.close()     return result  async def http_handler(request: Request) -&gt; Response:     loop = asyncio.get_running_loop()     image = await request.read()      # store image to shared memory     shm = shared_memory.SharedMemory(name='shared-mem-for-image', create=True, size=len(image))     shm.buf[:len(image)] = image     result = await loop.run_in_executor(executor, detect_image, shm)     shm.close()     shm.unlink()     return Response(result)<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b <code>close()<\/code> \u0438 <code>unlink()<\/code>.<\/p>\n<ul>\n<li>\n<p>\u043c\u0435\u0442\u043e\u0434 <code>close()<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e;<\/p>\n<\/li>\n<li>\n<p>\u043c\u0435\u0442\u043e\u0434 <code>unlink()<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u0415\u0441\u043b\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0443\u0442\u0435\u0447\u0435\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435\u0442 \u0438 \u0441\u0435\u0440\u0432\u0438\u0441 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u043e.<\/p>\n<blockquote>\n<p>\u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432, \u043d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438. \u041f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u043e\u0434\u043e\u0432\u043e\u0439 \u0431\u0430\u0437\u0435 \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e-\u0442\u043e \u0433\u0434\u0435-\u0442\u043e \u043d\u0435 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u043b\u0438, \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043b\u0438\u0448\u043d\u0438\u0439 \u0440\u0430\u0437.<\/p>\n<\/blockquote>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0439\u0442\u0438 \u0438\u0441\u043a\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0442\u043e \u0432 <a href=\"https:\/\/docs.python.org\/3\/library\/multiprocessing.shared_memory.html#multiprocessing.managers.SharedMemoryManager\"><u>\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/u><\/a> python \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043a\u043b\u0430\u0441\u0441 <code>SharedMemoryManager<\/code>.<\/p>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043e\u0447\u0438\u0449\u0430\u0442\u044c \u0435\u0435 \u0432 \u043d\u0443\u0436\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. \u0421\u0430\u043c \u043a\u043b\u0430\u0441\u0441 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b <code>start()<\/code> \u0438 <code>shutdown()<\/code> \u0432\u0440\u0443\u0447\u043d\u0443\u044e. \u041f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u043c \u043f\u0440\u0430\u0432\u0434\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u0430\u043c\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0441\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0432 \u043d\u0443\u0436\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u0435\u0442\u043e\u0434 <code>unlink()<\/code>.<\/p>\n<pre><code class=\"python\">from concurrent.futures import ProcessPoolExecutor from multiprocessing.managers import SharedMemoryManager  def detect_image(shm) -&gt; str:     image = shm.buf     # cpu-bound task ...     return 'cat'  async def http_handler(request: Request) -&gt; Response:     loop = asyncio.get_running_loop()     image = await request.read()      # store image to shared memory     with SharedMemoryManager() as manager:         shm = manager.SharedMemory(size=len(image))         shm.buf[:len(image)] = image         result = await loop.run_in_executor(executor, detect_image, shm)      return Response(result)<\/code><\/pre>\n<p>\u0423 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0438\u043d\u0443\u0441\u043e\u0432:<\/p>\n<ul>\n<li>\n<p><code>SharedMemoryManager<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0432\u0441\u0435 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c <code>Pipe<\/code> \u0438\u043b\u0438 \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u0441\u043e\u043a\u0435\u0442\u0430, \u0447\u0442\u043e \u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u043b <code>SharedMemoryManager<\/code>. \u0422\u043e \u0435\u0441\u0442\u044c \u0442\u0430\u043c \u0433\u0434\u0435 \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 <code>start()<\/code> \u0438\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u044b\u0439 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0430\u043c\u043e\u043c\u0443 \u043d\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u043d\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442.<\/p>\n<p><a class=\"anchor\" name=\"section4\" id=\"section4\"><\/a><\/p>\n<h3>\u00ab\u0412\u043b\u0430\u0434\u0435\u043d\u0438\u0435\u00bb \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0438\u0434\u0435\u0435\u0439 \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u044f (ownership) \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u043c.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430.<\/p>\n<p>\u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c 2 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u2013 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0438 \u043f\u043e\u0434\u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a. \u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043e\u043d\u0438 \u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f. \u041f\u0440\u043e\u0446\u0435\u0441\u0441-\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443, \u0430 \u043e\u0442\u0434\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u043e\u0442\u0432\u0435\u0442\u043e\u043c.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0431\u043b\u043e\u043a \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c (\u0442\u043e \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441), \u0442\u043e \u043c\u044b \u0442\u0435\u0440\u044f\u0435\u043c \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u043d\u0430\u0434 \u044d\u0442\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e. \u0415\u0441\u043b\u0438 \u043c\u044b \u043d\u0435 \u0432\u043b\u0430\u0434\u0435\u0435\u043c \u044d\u0442\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0437\u043e\u0432\u0435\u0442\u0441\u044f \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>__del__<\/code>, \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u043c.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u043c\u044b \u0432\u043b\u0430\u0434\u0435\u0435\u043c<\/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-443039","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/443039","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=443039"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/443039\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=443039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=443039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=443039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}