{"id":765,"date":"2012-09-24T10:15:29","date_gmt":"2012-09-24T06:15:29","guid":{"rendered":"http:\/\/seriyps.ru\/blog\/?p=765"},"modified":"2012-09-24T05:28:04","modified_gmt":"2012-09-24T01:28:04","slug":"razdacha-bolshix-fajlov-cherez-server-tornadoweb","status":"publish","type":"post","link":"https:\/\/seriyps.com\/blog\/2012\/09\/24\/razdacha-bolshix-fajlov-cherez-server-tornadoweb\/","title":{"rendered":"\u0420\u0430\u0437\u0434\u0430\u0447\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0441\u0435\u0440\u0432\u0435\u0440 TornadoWEB"},"content":{"rendered":"<p><a title=\"TornadoWEB webserver\" href=\"http:\/\/www.tornadoweb.org\/\">TornadoWeb<\/a> &#8211; \u044d\u0442\u043e \u043b\u0435\u0433\u043a\u043e\u0432\u0435\u0441\u043d\u044b\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 Python \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0439 \u043d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0441\u043e\u043a\u0435\u0442\u044b \u0438 epool\/kqueue \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0442\u044c\u044e.\u00a0\u0421\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0439. \u041f\u0440\u0438\u0432\u043b\u0435\u043a\u0430\u0435\u0442 \u0441\u0432\u043e\u0438\u043c \u043a\u0440\u0430\u0439\u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u043c API (\u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0442\u0435\u043c-\u0436\u0435 Twisted) \u0438 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c &#8211; &#8220;\u0437\u0430\u0447\u0435\u043c \u0440\u0430\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0444\u0430\u0439\u043b\u044b \u0447\u0435\u0440\u0435\u0437 Tornado?&#8221; \u0412\u0435\u0434\u044c \u0434\u0430\u0436\u0435 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f <a href=\"http:\/\/www.tornadoweb.org\/documentation\/overview.html#static-files-and-aggressive-file-caching\">\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442<\/a> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e Nginx. \u0427\u0442\u043e-\u0436, \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0442\u0430\u043a \u0438 \u0435\u0441\u0442\u044c. \u041d\u043e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435. \u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0447\u0435\u0440\u0435\u0437 Tornado \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0440\u0430\u0437\u0434\u0430\u0432\u0430\u043b\u043e\u0441\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043c\u0435\u043b\u043a\u0438\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u0435\u043a, \u0445\u0440\u0430\u043d\u044f\u0449\u0438\u0445\u0441\u044f \u0432 SQLite \u0438 \u043e\u0434\u0438\u043d \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0444\u0430\u0439\u043b \u043d\u0430 200\u041c\u0411 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0432\u0441\u0435\u0445 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 URL. \u041f\u043e\u0434\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0430\u0434\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u044d\u0442\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 Nginx \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c.<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0432\u043e\u043f\u0440\u043e\u0441 &#8211; &#8220;\u043d\u0443 \u0442\u0430\u043a \u0432 \u0447\u0435\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 &#8211; \u0440\u0430\u0437\u0434\u0430\u0432\u0430\u0439 \u043d\u0430 \u0437\u0434\u043e\u0440\u043e\u0432\u044c\u0435!&#8221;. \u0412\u043e\u0442 \u0442\u0443\u0442 \u043c\u044b \u0438 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u0441 \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e\u0439 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 &#8211; \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 <code>StaticFileHandler<\/code>\u00a0\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0432\u0435\u0441\u044c \u0444\u0430\u0439\u043b <strong>\u0446\u0435\u043b\u0438\u043a\u043e\u043c \u0432 \u043f\u0430\u043c\u044f\u0442\u044c<\/strong> \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043e\u0442\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0443 (<a title=\"Tornado loads full file content in memory.\" href=\"https:\/\/github.com\/facebook\/tornado\/blob\/v2.4.0\/tornado\/web.py#L1593\">\u043f\u0440\u0443\u0444\u043b\u0438\u043d\u043a<\/a>). \u041f\u043e\u043c\u0438\u043c\u043e \u044d\u0442\u043e\u0433\u043e, \u0437\u0430\u043d\u044f\u0442\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043a\u043b\u0438\u0435\u043d\u0442 \u0440\u0430\u0437\u043e\u0440\u0432\u0430\u043b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u043a\u0430\u0447\u0430\u0432 \u0432\u0435\u0441\u044c \u0444\u0430\u0439\u043b \u0446\u0435\u043b\u0438\u043a\u043e\u043c.<\/p>\n<p>\u0412\u043e\u0442 \u044d\u0442\u0438 2 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0438 \u0431\u0443\u0434\u0435\u043c \u0440\u0435\u0448\u0430\u0442\u044c.<!--more--><\/p>\n<h3>\u0414\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b<\/h3>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u0441\u0442\u0435\u043d\u0434:<\/p>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c Tornado<\/p>\n<pre><code class=\"bash\">mkdir tornado-bigfile\ncd tornado-bigfile\/\nvirtualenv .env\nsource .env\/bin\/activate\npip install tornado<\/code><\/pre>\n<p>\u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0444\u0430\u0439\u043b<\/p>\n<pre><code class=\"bash\">head -c 100M \/dev\/urandom &gt; static_file.bin<\/code><\/pre>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0444\u0430\u0439\u043b-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 Tornado<\/p>\n<pre><code class=\"python\"># file=server.py\n# -*- coding: utf8 -*-\nimport tornado.ioloop\nimport tornado.web\n\napplication = tornado.web.Application([\n        (r&quot;\/(big_file.bin)&quot;, tornado.web.StaticFileHandler, {&quot;path&quot;: &quot;.&quot;}),\n])\n\nif __name__ == &quot;__main__&quot;:\n    application.listen(8888)\n    tornado.ioloop.IOLoop.instance().start()<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c<\/p>\n<pre>python server.py<\/pre>\n<p>\u041f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0444\u0430\u0439\u043b<\/p>\n<pre><code class=\"python\">from urllib2 import urlopen\nhost = &quot;http:\/\/localhost:8888&quot;\nprint len(urlopen(host + &quot;\/big_file.bin&quot;).read())<\/code><\/pre>\n<p>\u0412\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e. \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432<\/p>\n<pre>$ ps xa -o rss,command | grep server.py\r\n110784 python server.py<\/pre>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 100\u041c\u0411! \u0422\u043e \u0435\u0441\u0442\u044c \u0432\u0435\u0441\u044c \u0444\u0430\u0439\u043b \u0434\u0430\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u0434\u0430\u0447\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0432\u0438\u0441\u0438\u0442 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u041d\u043e \u044d\u0442\u043e \u0435\u0449\u0435 \u043c\u0435\u043b\u043e\u0447\u0438. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043d\u0435 \u0432\u0435\u0441\u044c \u0444\u0430\u0439\u043b, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0432\u044b\u0445 \u0431\u0430\u0439\u0442<\/p>\n<pre><code class=\"python\">[urlopen(host + &quot;\/big_file.bin&quot;).read(1) for _ in xrange(2000)]<\/code><\/pre>\n<p>\u041c\u0430\u043b\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0440\u0430\u0439\u043d\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e, \u0442\u0430\u043a \u0435\u0449\u0435 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u043e\u0436\u0440\u0430\u043b 3.5\u0413\u0411 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0430 \u043c\u043e\u0435\u0439 \u043c\u0430\u0448\u0438\u043d\u0435<\/p>\n<pre>$ ps xa -o rss,command | grep server.py\r\n3587920 python server.py<\/pre>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u044d\u0442\u043e \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442.<\/p>\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435 &#8211; \u0431\u0443\u0434\u0435\u043c \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u043f\u043e \u043c\u0435\u0440\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u0421\u0447\u0438\u0442\u0430\u043b\u0438 0.5\u041c\u0411 &#8211; \u0441\u043a\u043e\u0440\u043c\u0438\u043b\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0443, \u0441\u0447\u0438\u0442\u0430\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u0443\u0441\u043e\u043a, \u0441\u043a\u043e\u0440\u043c\u0438\u043b\u0438&#8230; \u0418 \u0442\u0430\u043a \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0442\u0434\u0430\u0434\u0438\u043c \u0432\u0435\u0441\u044c \u0444\u0430\u0439\u043b. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0412 Tornado \u044d\u0442\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u043c <code>@tornado.web.asynchronous<\/code> \u0432 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0438 \u0441 <code>self.flush()<\/code>. \u041d\u0443 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u0432 \u043f\u043e \u0432\u043a\u0443\u0441\u0443. \u0412 \u0438\u0434\u0435\u0430\u043b\u0435, \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043d\u0430\u0448\u0435\u0439 <code>get()<\/code> \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0438\u0437 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e Tornado, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441\u043e \u0441\u0442\u0440\u043e\u043a\u0438 <a href=\"https:\/\/github.com\/facebook\/tornado\/blob\/v2.4.0\/tornado\/web.py#L1544\">1544<\/a> \u0438 \u0434\u043e <a href=\"https:\/\/github.com\/facebook\/tornado\/blob\/v2.4.0\/tornado\/web.py#L1591\">1591<\/a>, \u043d\u043e \u043c\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u043c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u043e\u0439 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 <code>Content-Length<\/code>.<\/p>\n<pre><code class=\"python\">import os\n\nclass FileStreamerHandler(tornado.web.RequestHandler):\n    CHUNK_SIZE = 512000         # 0.5 MB\n\n    def initialize(self, file_path):\n        self.path = file_path\n\n    @tornado.web.asynchronous  # \u043d\u0435 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0441\u043e\u043a\u0435\u0442 \u043a\u043e\u0433\u0434\u0430 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f (\u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0442\u044c self.finish())\n    def get(self):\n        self.set_header(&quot;Content-Length&quot;, os.path.getsize(self.path))\n        self._fd = open(self.path, &quot;rb&quot;)\n        self.flush()  # \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438\n        self.write_more()\n\n    def write_more(self):\n        data = self._fd.read(self.CHUNK_SIZE)\n        if not data:  # \u0435\u0441\u043b\u0438 \u0432\u0435\u0441\u044c \u0444\u0430\u0439\u043b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d...\n            self.finish()  # \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u043c \u0431\u0443\u0444\u0435\u0440 \u0438 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043a\u0435\u0442\n            self._fd.close()  # \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0444\u0430\u0439\u043b\n            return\n        self.write(data)\n        # \u0437\u0430\u0446\u0438\u043a\u043b\u0438\u0432\u0430\u0435\u043c\u0441\u044f - \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c write_more \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0443\u0439\u0434\u0443\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0443\n        self.flush(callback=self.write_more)\n\napplication = tornado.web.Application([\n        #...\n        (r&quot;\/big_stream.bin&quot;, FileStreamerHandler,\n         {&quot;file_path&quot;: &quot;big_file.bin&quot;}),\n])<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0431\u0443\u0435\u043c<\/p>\n<pre><code class=\"python\">[urlopen(host + &quot;\/big_stream.bin&quot;).read() for _ in xrange(2000)]\n[urlopen(host + &quot;\/big_stream.bin&quot;).read(1) for _ in xrange(2000)]<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0447\u0430\u0435\u043c, \u0447\u0442\u043e \u0432\u0442\u043e\u0440\u0430\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0430 \u043c\u043e\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u043e!<\/p>\n<pre>ps xa -o rss,command | grep server.py\r\n29684 python server.py<\/pre>\n<p>\u0420\u0430\u0437\u043c\u0435\u0440 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u0447\u0442\u0438 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f!<\/p>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430 \u0440\u0435\u0448\u0435\u043d\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0435\u0449\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043a\u043e\u0434, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u0431\u0435\u0437 \u043a\u043e\u043b\u043b\u0431\u0435\u043a\u043e\u0432. \u041d\u0443 \u0432 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u0443 \u043d\u0430\u0441 \u0436\u0435 Python, \u0430 \u043d\u0435 NodeJS \u043a\u0430\u043a\u043e\u0439-\u0442\u043e!<\/p>\n<h3>\u0414\u0435\u043b\u0430\u0435\u043c \u043a\u0440\u0430\u0441\u0438\u0432\u043e<\/h3>\n<p>\u0418\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043a\u043e\u043b\u043b\u0431\u0435\u043a\u043e\u0432 \u043d\u0430\u043c \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0441\u043e-\u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u0432 \u0432\u0438\u0434\u0435 \u043f\u0438\u0442\u043e\u043d\u044c\u0435\u0433\u043e <code>yield<\/code> \u0432 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 \u0441 \u043c\u043e\u0434\u0443\u043b\u0435\u043c <a title=\"tornado.gen\" href=\"http:\/\/www.tornadoweb.org\/documentation\/gen.html\">tornado.gen<\/a>. \u042d\u0442\u043e\u0442 \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0438 \u043f\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 \u043a\u043e\u043b\u043b\u0431\u0435\u043a, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c &#8220;callback&#8221; (\u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0435\u0441\u0442\u044c \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438).<\/p>\n<pre><code class=\"python\">import tornado.gen\n\nclass GenFileStreamerHandler(tornado.web.RequestHandler):\n    CHUNK_SIZE = 512000         # 0.5 MB\n\n    def initialize(self, file_path):\n        self.path = file_path\n\n    @tornado.web.asynchronous\n    @tornado.gen.engine  # \u043e\u0442\u043c\u0435\u0447\u0430\u0435\u043c, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 yield-\u0438\u0442 Task-\u0438\n    def get(self):\n        self.set_header(&quot;Content-Length&quot;, os.path.getsize(self.path))\n        self.flush()\n        fd = open(self.path, &quot;rb&quot;)\n        data = fd.read(self.CHUNK_SIZE)\n        while data:\n            self.write(data)\n            yield tornado.gen.Task(self.flush)  # \u043d\u0435\u0442 \u043a\u043e\u043b\u043b\u0431\u0435\u043a\u0430\u043c!\n            data = fd.read(self.CHUNK_SIZE)\n        fd.close()\n        self.finish()\n\napplication = tornado.web.Application([\n        #...\n        (r&quot;\/big_stream_gen.bin&quot;, GenFileStreamerHandler,\n         {&quot;file_path&quot;: &quot;big_file.bin&quot;}),\n])<\/code><\/pre>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0447\u0438\u0442\u0430\u0435\u043c\u0435\u0435 \u0438 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u0435\u0435.<\/p>\n<p>\u0412\u0435\u0441\u044c \u043a\u043e\u0434 \u043e\u0434\u043d\u0438\u043c \u0444\u0430\u0439\u043b\u043e\u043c \u0435\u0441\u0442\u044c \u043d\u0430 <a href=\"https:\/\/gist.github.com\/3773703\">github gist<\/a>.<\/p>\n<p>\u041f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>with open():<\/code> \u0432\u043c\u0435\u0441\u0442\u043e <code>fd = open()<\/code> + <code>fd.close()<\/code>, \u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u043b\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u0438 \u043d\u0435 \u043f\u043e\u043b\u043d\u043e\u043c \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u0438 \u043e\u0442\u0432\u0435\u0442\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c. \u0412\u0438\u0434\u0438\u043c\u043e <code>with<\/code> \u0438 <code>yield<\/code> \u043f\u043b\u043e\u0445\u043e \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b. \u041a\u0442\u043e \u043f\u043e\u0434\u0441\u043a\u0430\u0436\u0435\u0442?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TornadoWeb &#8211; \u044d\u0442\u043e \u043b\u0435\u0433\u043a\u043e\u0432\u0435\u0441\u043d\u044b\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 Python \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0439 \u043d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0441\u043e\u043a\u0435\u0442\u044b \u0438 epool\/kqueue \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0442\u044c\u044e.\u00a0\u0421\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0439. \u041f\u0440\u0438\u0432\u043b\u0435\u043a\u0430\u0435\u0442 \u0441\u0432\u043e\u0438\u043c \u043a\u0440\u0430\u0439\u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u043c API (\u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0442\u0435\u043c-\u0436\u0435 Twisted) \u0438 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e. \u041f\u0435\u0440\u0432\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c &#8211; &#8220;\u0437\u0430\u0447\u0435\u043c \u0440\u0430\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0444\u0430\u0439\u043b\u044b \u0447\u0435\u0440\u0435\u0437 Tornado?&#8221; \u0412\u0435\u0434\u044c \u0434\u0430\u0436\u0435 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[214,249,212,213,122],"class_list":["post-765","post","type-post","status-publish","format-standard","hentry","category-python","tag-coroutines","tag-python","tag-tornado","tag-tornadoweb","tag-server"],"_links":{"self":[{"href":"https:\/\/seriyps.com\/blog\/wp-json\/wp\/v2\/posts\/765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seriyps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seriyps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seriyps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seriyps.com\/blog\/wp-json\/wp\/v2\/comments?post=765"}],"version-history":[{"count":10,"href":"https:\/\/seriyps.com\/blog\/wp-json\/wp\/v2\/posts\/765\/revisions"}],"predecessor-version":[{"id":775,"href":"https:\/\/seriyps.com\/blog\/wp-json\/wp\/v2\/posts\/765\/revisions\/775"}],"wp:attachment":[{"href":"https:\/\/seriyps.com\/blog\/wp-json\/wp\/v2\/media?parent=765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seriyps.com\/blog\/wp-json\/wp\/v2\/categories?post=765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seriyps.com\/blog\/wp-json\/wp\/v2\/tags?post=765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}