{"id":243207,"date":"2014-11-15T04:02:03","date_gmt":"2014-11-15T00:02:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=243207"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=243207","title":{"rendered":"<span class=\"post_title\">Python \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u044b event-driven \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c<\/span>"},"content":{"rendered":"<div class=\"content html_format\">     \t\u0421\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u043e \u0442\u043e, \u043a\u0430\u043a \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e<a href=\"http:\/\/legacy.python.org\/dev\/peps\/pep-0342\/\"> \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u044b\u0445 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 Python<\/a> \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c, \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044e \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u041f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u043a\u043e\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0435\u0433\u043e\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044f \u0432\u0430\u0441 \u043f\u0440\u0438\u044f\u0442\u043d\u043e \u0443\u0434\u0438\u0432\u0438\u0442 \u0438 \u043f\u0440\u043e\u044f\u0441\u043d\u0438\u0442 \u043d\u043e\u0432\u044b\u0435 \u0438 \u043c\u0430\u043b\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u044f\u0437\u044b\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0442\u0430\u043a\u0438\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b. \u0421\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0438 \u0441 \u0442\u0435\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u0432 \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u0445: <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\">asyncio<\/a>, <a href=\"http:\/\/www.tornadoweb.org\/en\/stable\/\">tornado<\/a>, etc.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u0422\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u0438 disclaimer<\/h4>\n<p>  \u041f\u043e\u043d\u044f\u0442\u0438\u0435 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0438\u043c\u0435\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0448\u0438\u0440\u043e\u043a\u043e\u0435 \u0442\u043e\u043b\u043a\u043e\u0432\u0430\u043d\u0438\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f, \u043a\u0430\u043a\u0438\u043c\u0438 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0430\u043c\u0438 \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u043b\u0430\u0434\u0430\u0442\u044c \u0432 \u043d\u0430\u0448\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:  <\/p>\n<ul>\n<li>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0432 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435;<\/li>\n<li>\u0412\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f;<\/li>\n<li>\u0412\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f;<\/li>\n<li>\u041c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044e.<\/li>\n<\/ul>\n<p>  \u041a\u0430\u043a \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c: <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A1%D0%BE%D0%B1%D1%8B%D1%82%D0%B8%D0%B9%D0%BD%D0%BE-%D0%BE%D1%80%D0%B8%D0%B5%D0%BD%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%BD%D0%BE%D0%B5_%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5\">\u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/a> \u0431\u0435\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0438 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%9C%D0%BD%D0%BE%D0%B3%D0%BE%D0%B7%D0%B0%D0%B4%D0%B0%D1%87%D0%BD%D0%BE%D1%81%D1%82%D1%8C#.D0.A1.D0.BE.D0.B2.D0.BC.D0.B5.D1.81.D1.82.D0.BD.D0.B0.D1.8F_.D0.B8.D0.BB.D0.B8_.D0.BA.D0.BE.D0.BE.D0.BF.D0.B5.D1.80.D0.B0.D1.82.D0.B8.D0.B2.D0.BD.D0.B0.D1.8F_.D0.BC.D0.BD.D0.BE.D0.B3.D0.BE.D0.B7.D0.B0.D0.B4.D0.B0.D1.87.D0.BD.D0.BE.D1.81.D1.82.D1.8C\">\u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u044c<\/a>. \u042d\u0444\u0444\u0435\u043a\u0442 \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0439 \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447, \u0440\u0435\u0430\u0433\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u043d\u0430 \u043d\u0435\u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u044e\u0449\u0438\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 I\/O: \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b, \u0438 \u0442. \u043f. \u0414\u0440\u0443\u0433\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u2014 \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0438 \u0440\u0430\u0441\u0447\u0435\u0442\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0435\u0440\u0441\u043e\u043d\u0430\u0436\u0435\u0439 \u0432 \u0438\u0433\u0440\u043e\u0432\u043e\u043c \u043c\u0438\u0440\u0435. \u041d\u043e \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u044f\u0442 \u0434\u043e\u043b\u0433\u0438\u0435 \u0440\u0430\u0441\u0447\u0435\u0442\u044b. <br \/>  \u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u0447\u0435\u0442\u043a\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0430\u044f\u0441\u044f \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0435 \u043f\u0440\u0435\u0440\u0432\u0430\u043b\u0430\u0441\u044c \u043d\u0430 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0430, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0435 \u0438\u043c\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0443\u0436\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e.<\/p>\n<h4>\u041e\u0441\u043d\u043e\u0432\u0430 \u0432\u0441\u0435\u0433\u043e<\/h4>\n<p>  \u0412 Python \u0445\u043e\u0440\u043e\u0448\u0435\u0439 \u043e\u0441\u043d\u043e\u0432\u043e\u0439 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u044d\u0442\u043e\u0433\u043e \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b, \u0435\u0441\u043b\u0438 \u0438\u0445 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u0432 \u043f\u0440\u044f\u043c\u043e\u043c \u0438 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u043d\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435. \u0422\u043e\u0447\u043d\u0435\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u044b\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b, API \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u0432 \u0432\u0435\u0440\u0441\u0438\u0438 Python 3.3. \u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 \u043d\u0435 \u0431\u044b\u043b\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f (\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430) \u043f\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044e \u0440\u0430\u0431\u043e\u0442\u044b \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0438 \u043d\u0435 \u0431\u044b\u043b\u043e \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0438\u0437 \u0434\u0440\u0443\u0433\u043e\u0433\u043e. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c \u0431\u044b\u043b\u0438 \u0438 \u0440\u0430\u043d\u044c\u0448\u0435, \u043d\u043e \u0438\u0437-\u0437\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u043e\u043d\u0438 \u0431\u044b\u043b\u0438 \u043d\u0435 \u0442\u0430\u043a \u00ab\u043a\u0440\u0430\u0441\u0438\u0432\u044b\u00bb \u043a\u0430\u043a \u0442\u043e, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0443 \u043d\u0430\u0441. \u041e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443 <a href=\"http:\/\/www.dabeaz.com\/coroutines\/\">\u00abA Curious Course on Coroutines and Concurrency\u00bb<\/a> \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0435\u0451 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a, \u0442\u0430\u043a \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438. \u0422\u0430\u043a\u043e\u0439 \u0433\u0434\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f coroutine \u0432 python \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u043d\u043e\u0432\u0448\u0435\u0441\u0442\u0432\u0430 \u0432 \u044f\u0437\u044b\u043a\u0435, \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438 \u0432 API Enhanced Python Generators. \u041d\u0438\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u044b\u0445 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f. <br \/>  \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0432 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u043d\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u0434\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0443 \u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435. \u0421\u043a\u043e\u043f\u0438\u0440\u0443\u0439\u0442\u0435 \u043a\u043e\u0434 \u043d\u0438\u0436\u0435 \u0432 \u043e\u043a\u043d\u043e \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python \u0432\u0435\u0440\u0441\u0438\u0438 3.3 \u0438 \u0432\u044b\u0448\u0435.  <\/p>\n<pre><code class=\"python\">def gen_factory():     state = None     while True:         print(&quot;state:&quot;, state)         state = yield state  gen = gen_factory() <\/code><\/pre>\n<p>  \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0441\u043e\u0437\u0434\u0430\u043d, \u0435\u0433\u043e \u043d\u0430\u0434\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c.   <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; next(gen) state: None <\/code><\/pre>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435. \u0418\u0437\u043c\u0435\u043d\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435:  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; gen.send(&quot;OK&quot;) state: OK 'OK' <\/code><\/pre>\n<p>  \u0412\u0438\u0434\u0438\u043c \u0447\u0442\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u043e \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u044b next \u0431\u0443\u0434\u0443\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0443\u0436\u0435 \u0435\u0433\u043e. <\/p>\n<h4>\u0417\u0430\u0447\u0435\u043c \u043d\u0430\u043c \u0432\u0441\u0435 \u044d\u0442\u043e?<\/h4>\n<p>  \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0437\u0430\u0434\u0430\u0447\u0443: \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u0432\u0435\u0442 \u041f\u0435\u0442\u0440\u043e\u0432\u0443 \u0440\u0430\u0437 \u0432 \u0434\u0432\u0435 \u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u0418\u0432\u0430\u043d\u043e\u0432\u0443 \u0440\u0430\u0437 \u0432 \u0442\u0440\u0438 \u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u0430 \u0432\u0441\u0435\u043c\u0443 \u043c\u0438\u0440\u0443 \u0440\u0430\u0437 \u0432 \u043f\u044f\u0442\u044c \u0441\u0435\u043a\u0443\u043d\u0434. \u0412 \u0432\u0438\u0434\u0435 Python \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"python\">def hello(name, timeout):     while True:         sleep(timeout)         print(&quot;\u041f\u0440\u0438\u0432\u0435\u0442, {}!&quot;.format(name))  hello(&quot;\u041f\u0435\u0442\u0440\u043e\u0432&quot;, 2.0) hello(&quot;\u0418\u0432\u0430\u043d\u043e\u0432&quot;, 3.0) hello(&quot;\u041c\u0438\u0440&quot;, 5.0) <\/code><\/pre>\n<p>  \u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0441\u044f \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0442\u044b \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u041f\u0435\u0442\u0440\u043e\u0432. \u041e\u0434\u043d\u0430\u043a\u043e! \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043d\u0435 \u0432\u043b\u0438\u044f\u044e\u0449\u0430\u044f \u043d\u0430 \u044f\u0441\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430, \u0430 \u0434\u0430\u0436\u0435 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442 \u2014 \u0443\u0442\u043e\u0447\u043d\u044f\u044e\u0449\u0430\u044f \u043d\u0430\u0448\u0443 \u043c\u044b\u0441\u043b\u044c, \u0438 \u044d\u0442\u043e \u0443\u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043a\u0430\u043a \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043e.  <\/p>\n<pre><code class=\"python\">@coroutine def hello(name, timeout):     while True:         yield from sleep(timeout)         print(&quot;\u041f\u0440\u0438\u0432\u0435\u0442, {}!&quot;.format(name))  hello(&quot;\u041f\u0435\u0442\u0440\u043e\u0432&quot;, 2.0) hello(&quot;\u0418\u0432\u0430\u043d\u043e\u0432&quot;, 3.0) hello(&quot;\u041c\u0438\u0440&quot;, 5.0) run() <\/code><\/pre>\n<p>  \u041a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0432 \u0441\u0442\u0438\u043b\u0435 pythonic way \u2014 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443, \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0431\u0435\u0437 \u043a\u0430\u043b\u0431\u044d\u043a\u043e\u0432, \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0438\u0445 \u043d\u0430\u0432\u043e\u0440\u043e\u0442\u043e\u0432 \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438, \u043b\u044e\u0431\u044b\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0432 \u043d\u0435\u043c \u0438\u0437\u043b\u0438\u0448\u043d\u0438. \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 coroutine, \u0441\u0432\u043e\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 sleep \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e run. \u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0431\u0435\u0437 \u043d\u0430\u0432\u043e\u0440\u043e\u0442\u043e\u0432 \u043d\u0435 \u043e\u0431\u043e\u0439\u0434\u0435\u0442\u0441\u044f. \u041d\u043e \u044d\u0442\u043e \u0442\u043e\u0436\u0435 pythonic way, \u043f\u0440\u044f\u0442\u0430\u0442\u044c \u0437\u0430 \u0444\u0430\u0441\u0430\u0434\u043e\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u0447\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u0432\u0441\u044e \u043c\u0430\u0433\u0438\u044e.<\/p>\n<h4>\u0421\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435<\/h4>\n<p>  \u041d\u0430\u0437\u043e\u0432\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u044c \u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043d\u0435\u0437\u0430\u0442\u0435\u0439\u043b\u0438\u0432\u043e \u2014 concurrency, \u0441\u043e \u0441\u043c\u044b\u0441\u043b\u043e\u043c \u0438 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0442\u043e\u0442 \u0444\u0430\u043a\u0442, \u0447\u0442\u043e \u044d\u0442\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u0438. \u041f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u0437 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e (\u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 next). \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u044f\u0437\u044b\u043a\u0430 yield from \u043f\u0440\u043e\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440. \u0422\u043e \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f sleep \u0434\u043e\u043b\u0436\u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0441\u043f\u0440\u044f\u0442\u0430\u0442\u044c \u0432\u0441\u044e \u043c\u0430\u0433\u0438\u044e. \u0412 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440, \u0435\u0435 \u0432\u044b\u0437\u0432\u0430\u0432\u0448\u0438\u0439, \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u0417\u0434\u0435\u0441\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0434 \u0442\u0443\u0442 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e \u0441\u0443\u0442\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u0447\u0442\u043e \u0442\u0430\u0439\u043c-\u0430\u0443\u0442 \u0438\u0441\u0442\u0435\u043a. \u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0436\u0435 \u0432\u0432\u043e\u0434\u0430-\u0432\u044b\u0432\u043e\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0438\u0434\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 (\u0447\u0442\u0435\u043d\u0438\u0435\/\u0437\u0430\u043f\u0438\u0441\u044c\/\u0442\u0430\u0439\u043c \u0430\u0443\u0442). \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u043c\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0442\u0438\u043f\u0430 sleep \u043c\u043e\u0433\u0443\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e yield from \u043b\u044e\u0431\u043e\u0439 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u0424\u0443\u043d\u043a\u0446\u0438\u044f run \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0435\u0433\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u2014 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0438\u0437\u0432\u043d\u0435 \u0438\/\u0438\u043b\u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0435\u043b\u044f \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c.<br \/>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430:  <\/p>\n<pre><code class=\"python\">class coroutine(object):     &quot;&quot;&quot;\u0414\u0435\u043b\u0430\u0435\u0442 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u043d\u0430 \u0431\u0430\u0437\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u043e\u0433\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430.&quot;&quot;&quot;     _current = None      def __init__(self, callable):         self._callable = callable      def __call__(self, *args, **kwargs):         corogen = self._callable(*args, **kwargs)         cls = self.__class__         if cls._current is None:             try:                 cls._current = corogen                 next(corogen)             finally:                 cls._current = None         return corogen <\/code><\/pre>\n<p>  \u041e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d \u0432 \u0432\u0438\u0434\u0435 \u043a\u043b\u0430\u0441\u0441\u0430, \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u0435\u043c, \u043a\u0430\u043a \u0438 \u043e\u0431\u0435\u0449\u0430\u043b, \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440. \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0441 _current \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0430 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430, \u0435\u0441\u043b\u0438 \u0434\u0435\u043a\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0430\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0435\u043b\u0430 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0438 \u0442\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u043d. \u0422\u0430\u043a \u0436\u0435 \u044d\u0442\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u0432 \u043a\u0430\u043a\u043e\u0439 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u043e \u043f\u043e\u043f\u0430\u043b\u043e \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435 \u0432 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 sleep.  <\/p>\n<pre><code class=\"python\">def sleep(timeout):     &quot;&quot;&quot;\u041f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u044f &quot;\u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u0438\u0441\u0442\u0435\u043a&quot;.&quot;&quot;&quot;     corogen = coroutine._current     dispatcher.setup_timeout(corogen, timeout)     revent = yield     return revent <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0432\u0438\u0434\u0438\u043c \u0432\u044b\u0437\u043e\u0432 dispatcher.setup_sleep, \u044d\u0442\u043e \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0443 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0447\u0442\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0442\u0430\u043a\u043e\u0439-\u0442\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u00ab\u0442\u0430\u0439\u043c-\u0430\u0443\u0442\u00bb \u043f\u043e \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044e \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c timeout \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0435\u043a\u0443\u043d\u0434.   <\/p>\n<pre><code class=\"python\">from collections import deque from time import time, sleep as sys_sleep   class Dispatcher(object):     &quot;&quot;&quot;\u041e\u0431\u044a\u0435\u043a\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0441\u043f\u0435\u0447\u0435\u0440 \u0441\u043e\u0431\u044b\u0442\u0438\u0439.&quot;&quot;&quot;     def __init__(self):         self._pending = deque()         self._deadline = time() + 3600.0      def setup_timeout(self, corogen, timeout):         deadline = time() + timeout         self._deadline = min([self._deadline, deadline])         self._pending.append([corogen, deadline])         self._pending = deque(sorted(self._pending, key=lambda a: a[1]))      def run(self):         &quot;&quot;&quot;\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0446\u0438\u043a\u043b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439.&quot;&quot;&quot;         while len(self._pending) &gt; 0:             timeout = self._deadline - time()             self._deadline = time() + 3600.0             if timeout &gt; 0:                 sys_sleep(timeout)             while len(self._pending) &gt; 0:                 if self._pending[0][1] &lt;= time():                     corogen, _ = self._pending.popleft()                     try:                         coroutine._current = corogen                         corogen.send(&quot;timeout&quot;)                     except StopIteration:                         pass                     finally:                         coroutine._current = None                 else:                     break  dispatcher = Dispatcher() run = lambda: dispatcher.run() <\/code><\/pre>\n<p>  \u0412 \u043a\u043e\u0434\u0435 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0442\u043e\u0436\u0435 \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435\u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e. \u041a\u0443\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043a\u043b\u0430\u0441\u0441\u0430 coroutine._current. \u041f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u043c\u043e\u0434\u0443\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430, \u0432 \u0440\u0430\u0431\u043e\u0447\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u0442\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0441\u0438\u043d\u0433\u043b\u0435\u0442\u043e\u043d. \u041a\u043b\u0430\u0441\u0441 collections.deque \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043d \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u043f\u0438\u0441\u043a\u0430, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0438 \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u0441\u0432\u043e\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c popleft. \u041d\u0443 \u0432\u043e\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438 \u0432\u0441\u0435, \u0438 \u043d\u0435\u0442 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043e\u0441\u043e\u0431\u043e\u0439 \u043c\u0430\u0433\u0438\u0438. \u0412\u0441\u044f \u043e\u043d\u0430 \u043d\u0430 \u043f\u043e\u0432\u0435\u0440\u043a\u0443 \u0441\u043f\u0440\u044f\u0442\u0430\u043d\u0430 \u0435\u0449\u0435 \u0433\u043b\u0443\u0431\u0436\u0435, \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u044b\u0445 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 Python. \u0418\u0445 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0424\u0430\u0439\u043b: concurrency.py<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\"># concurrency.py from collections import deque from time import time, sleep as sys_sleep   class coroutine(object):     &quot;&quot;&quot;\u0414\u0435\u043b\u0430\u0435\u0442 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u043d\u0430 \u0431\u0430\u0437\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u043e\u0433\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430.&quot;&quot;&quot;     _current = None      def __init__(self, callable):         self._callable = callable      def __call__(self, *args, **kwargs):         corogen = self._callable(*args, **kwargs)         cls = self.__class__         if cls._current is None:             try:                 cls._current = corogen                 next(corogen)             finally:                 cls._current = None         return corogen   def sleep(timeout):     &quot;&quot;&quot;\u041f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u044f &quot;\u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u0438\u0441\u0442\u0435\u043a&quot;.&quot;&quot;&quot;     corogen = coroutine._current     dispatcher.setup_timeout(corogen, timeout)     revent = yield     return revent   class Dispatcher(object):     &quot;&quot;&quot;\u041e\u0431\u044a\u0435\u043a\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0441\u043f\u0435\u0447\u0435\u0440 \u0441\u043e\u0431\u044b\u0442\u0438\u0439.&quot;&quot;&quot;     def __init__(self):         self._pending = deque()         self._deadline = time() + 3600.0      def setup_timeout(self, corogen, timeout):         deadline = time() + timeout         self._deadline = min([self._deadline, deadline])         self._pending.append([corogen, deadline])         self._pending = deque(sorted(self._pending, key=lambda a: a[1]))      def run(self):         &quot;&quot;&quot;\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0446\u0438\u043a\u043b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439.&quot;&quot;&quot;         while len(self._pending) &gt; 0:             timeout = self._deadline - time()             self._deadline = time() + 3600.0             if timeout &gt; 0:                 sys_sleep(timeout)             while len(self._pending) &gt; 0:                 if self._pending[0][1] &lt;= time():                     corogen, _ = self._pending.popleft()                     try:                         coroutine._current = corogen                         corogen.send(&quot;timeout&quot;)                     except StopIteration:                         pass                     finally:                         coroutine._current = None                 else:                     break  dispatcher = Dispatcher() run = lambda: dispatcher.run() <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0424\u0430\u0439\u043b: sample.py<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\"># sample.py from concurency import coroutine, sleep, run  @coroutine def hello(name, timeout):     while True:         yield from sleep(timeout)         print(&quot;\u041f\u0440\u0438\u0432\u0435\u0442, {}!&quot;.format(name))  hello(&quot;\u041f\u0435\u0442\u0440\u043e\u0432&quot;, 2.0) hello(&quot;\u0418\u0432\u0430\u043d\u043e\u0432&quot;, 3.0) hello(&quot;\u041c\u0438\u0440&quot;, 5.0) run() <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<h4>Outro<\/h4>\n<p>  \u0415\u0441\u043b\u0438 \u0442\u0435\u043c\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f, \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0432 \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432\u0432\u043e\u0434\u0430\/\u0432\u044b\u0432\u043e\u0434\u0430 \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c TCP Echo \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430. \u0421 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u043c \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u043e\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0432 \u0432\u0438\u0434\u0435 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u043c, \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u043e\u043c, \u0447\u0435\u043c Python \u044f\u0437\u044b\u043a\u0435.      \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/243207\/\"> http:\/\/habrahabr.ru\/post\/243207\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">     \t\u0421\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u043e \u0442\u043e, \u043a\u0430\u043a \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e<a href=\"http:\/\/legacy.python.org\/dev\/peps\/pep-0342\/\"> \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u044b\u0445 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 Python<\/a> \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c, \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044e \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u041f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u043a\u043e\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0435\u0433\u043e\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044f \u0432\u0430\u0441 \u043f\u0440\u0438\u044f\u0442\u043d\u043e \u0443\u0434\u0438\u0432\u0438\u0442 \u0438 \u043f\u0440\u043e\u044f\u0441\u043d\u0438\u0442 \u043d\u043e\u0432\u044b\u0435 \u0438 \u043c\u0430\u043b\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u044f\u0437\u044b\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0442\u0430\u043a\u0438\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b. \u0421\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0438 \u0441 \u0442\u0435\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u0432 \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u0445: <a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\">asyncio<\/a>, <a href=\"http:\/\/www.tornadoweb.org\/en\/stable\/\">tornado<\/a>, etc.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-243207","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/243207","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=243207"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/243207\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=243207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=243207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=243207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}