{"id":268225,"date":"2015-11-15T14:00:04","date_gmt":"2015-11-15T11:00:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=268225"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=268225","title":{"rendered":"\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 Python 3.5 \u0438 Mongodb"},"content":{"rendered":"<p>       <i>\u042d\u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u043e\u043b\u044c\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043e\u0431 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043d\u043e\u0432\u0448\u0435\u0441\u0442\u0432\u0430\u0445 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u0430 \u0434\u043b\u044f <code>mongodb<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u0432 <code>tornado<\/code>. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043c\u043e\u0442\u0438\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0441\u043b\u0443\u0436\u0438\u043b \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430 \u2014 \u043d\u043e\u0432\u0448\u0435\u0441\u0442\u0432\u0430, \u043f\u043e\u044f\u0432\u0438\u0432\u0448\u0438\u0435\u0441\u044f \u0432 \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 <code>asyncio<\/code>, <code>async<\/code>, <code>await<\/code> \u0438 <code>Python 3.5<\/code>. \u0421\u0430\u043c\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0435 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u0448\u0435\u0441\u0442\u0432, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <code>MongoDB<\/code>. <br \/>  <\/i><\/p>\n<p>  <a href=\"#0\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/a><br \/>  <a href=\"#1\"> asyncio<\/a><br \/>  <a href=\"#2\"> aggregate<\/a><br \/>  <a href=\"#3\"> Python 3.5 <\/a><br \/>  <a href=\"#4\"> async and await<\/a><\/p>\n<p>  <a name=\"habracut\"><\/a>  <\/p>\n<h3><a name=\"0\"><\/a><font color=\"orange\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/font><\/h3>\n<p>  \u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0431\u044b\u043b\u0430 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u0430 \u043d\u043e\u0432\u0430\u044f <code>Beta<\/code> \u0432\u0435\u0440\u0441\u0438\u044f <code>Python<\/code> \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u0430 \u0434\u043b\u044f <code>Mongodb<\/code>, <code>Motor<\/code>. \u0412 \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043e\u0434\u043d\u043e \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0439. \u0414\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c:<br \/>  <code> python -m pip install --pre motor==0.5b0 <\/code><br \/>  <code>Motor 0.5<\/code> \u043f\u043e \u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 <code>PyMongo 2.8.0<\/code>. \u042d\u0442\u043e \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f <code>PyMongo<\/code>, \u043d\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u043d\u0435 \u0431\u044b\u043b\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0447\u0442\u043e\u0431 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043d\u0430 \u0442\u0440\u0435\u0442\u044c\u044e \u0432\u0435\u0440\u0441\u0438\u044e, \u0447\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e\u0442 \u0440\u0435\u043b\u0438\u0437 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0439.<\/p>\n<h3><a name=\"1\"><\/a><font color=\"orange\">asyncio<\/font><\/h3>\n<p>  <code>Motor<\/code> \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u0442 \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441 <code>asyncio<\/code>, \u043a\u0430\u043a \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 <code>Tornado<\/code>. \u0411\u043e\u043b\u044c\u0448\u0430\u044f \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u043d\u043e\u0441\u0442\u044c \u0420\u0435\u043c\u0438 \u0414\u0436\u043e\u043b\u0438\u043d, \u0410\u043d\u0434\u0440\u0435\u0439 \u0421\u0432\u0435\u0442\u043b\u043e\u0432  <a href=\"http:\/\/habrahabr.ru\/users\/svetlov\/\" class=\"user_link\">svetlov<\/a> \u0438 \u041d\u0438\u043a\u043e\u043b\u0430\u0439 \u041d\u043e\u0432\u0438\u043a \u0437\u0430 \u0438\u0445 \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0439 \u0432\u043a\u043b\u0430\u0434 \u0432 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e <code>Motor<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <code>asyncio<\/code>.<\/p>\n<p>  API-\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b <code>Tornado<\/code> \u0438 <code>asyncio<\/code> \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0440\u043e\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438. \u041f\u0440\u0438\u043c\u0435\u0440 <code>Motor<\/code> \u0441 <code>Tornado<\/code>:  <\/p>\n<pre><code class=\"python\"># Tornado API from tornado import gen, ioloop from motor.motor_tornado import MotorClient  @gen.coroutine def f():     result = yield client.db.collection.insert({'_id': 1})     print(result)  client = MotorClient() ioloop.IOLoop.current().run_sync(f) <\/code><\/pre>\n<p>  \u0418 \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f asyncio:  <\/p>\n<pre><code class=\"python\">import asyncio from motor.motor_asyncio import AsyncIOMotorClient  @asyncio.coroutine def f():     result = yield from client.db.collection.insert({'_id': 1})     print(result)  client = AsyncIOMotorClient() asyncio.get_event_loop().run_until_complete(f()) <\/code><\/pre>\n<p>  \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 <code>Tornado<\/code>, <code>asyncio<\/code> \u043d\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e <code>http<\/code>, \u0430 \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <code>aiohttp<\/code> \u0410\u043d\u0434\u0440\u0435\u044f \u0421\u0432\u0435\u0442\u043b\u043e\u0432\u0430. \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 <a href=\"http:\/\/motor.readthedocs.org\/en\/latest\/tutorial-asyncio.html#a-web-application-with-aiohttp\">\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b Motor \u0441 aiohttp<\/a>.<\/p>\n<h3><a name=\"2\"><\/a><font color=\"orange\">aggregate<\/font><\/h3>\n<p>  <a href=\"http:\/\/motor.readthedocs.org\/en\/latest\/api\/motor_collection.html#motor.motor_tornado.MotorCollection.aggregate\">MotorCollection.aggregate<\/a> \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0443\u0440\u0441\u043e\u0440, \u0438 \u043a\u0443\u0440\u0441\u043e\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0431\u0435\u0437 <code>yield<\/code>. \u0421\u0442\u0430\u0440\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f:  <\/p>\n<pre><code class=\"python\"># Motor 0.4 and older, no longer supported. cursor = yield collection.aggregate(pipeline, cursor={}) while (yield cursor.fetch_next):     doc = cursor.next_object()     print(doc) <\/code><\/pre>\n<p>  \u0412 <code>Motor 0.5<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0439\u0442\u0435:  <\/p>\n<pre><code class=\"python\"># Motor 0.5: no &quot;cursor={}&quot;, no &quot;yield&quot;. cursor = collection.aggregate(pipeline) while (yield cursor.fetch_next):     doc = cursor.next_object()     print(doc) <\/code><\/pre>\n<p>  \u0412 <code>asyncio<\/code> \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>yield from<\/code>:  <\/p>\n<pre><code class=\"python\"># Motor 0.5 with asyncio. cursor = collection.aggregate(pipeline) while (yield from cursor.fetch_next):     doc = cursor.next_object()     print(doc) <\/code><\/pre>\n<p>  <\/p>\n<h3><a name=\"3\"><\/a><font color=\"orange\">Python 3.5<\/font><\/h3>\n<p>  \u0421\u0435\u0439\u0447\u0430\u0441 <code>Motor<\/code> \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c \u0441 <code>Python 3.5<\/code>, \u0447\u0442\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0445 \u0443\u0441\u0438\u043b\u0438\u0439. \u042d\u0442\u043e \u0431\u044b\u043b\u043e \u0442\u0440\u0443\u0434\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>Motor<\/code> \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u043c\u0438 (coroutines), \u043e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0438\u0437 \u0441\u0432\u043e\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a <a href=\"http:\/\/motor.readthedocs.org\/en\/latest\/api\/motor_client.html#motor.motor_tornado.MotorClient.open\">MotorClient.open<\/a> \u0438 <a href=\"http:\/\/motor.readthedocs.org\/en\/latest\/api\/gridfs.html#motor.motor_tornado.MotorGridFS.put\">MotorGridFS.put<\/a>.<br \/>  \u0411\u044b\u043b \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432 <code>Python 2.6<\/code> c <code>Python 3.4<\/code>, \u043d\u043e \u0432 <code>Python 3.5<\/code> \u044d\u0442\u043e \u0431\u044b\u043b\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043b\u043e\u043c\u0430\u043d\u043e. \u041d\u0435\u0442 \u0435\u0434\u0438\u043d\u043e\u0433\u043e \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0438\u0437 <code>Python 3.5<\/code> \u043d\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u0438\u043b\u0438 <code>Python 2<\/code> \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0431\u0430\u0437\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e\u0441\u044f \u043d\u0430 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435, \u0442\u0430\u043a \u0447\u0442\u043e \u0432\u0441\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b <code>motor<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043d\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432.<\/p>\n<h3><a name=\"4\"><\/a><font color=\"orange\">async and await<\/font><\/h3>\n<p>  \u041d\u0430\u0433\u0440\u0430\u0434\u0430 \u0437\u0430 \u0443\u0441\u0438\u043b\u0438\u044f \u043f\u043e\u0442\u0440\u0430\u0447\u0435\u043d\u043d\u044b\u0435 \u043d\u0430 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e \u0441 <code>Python 3.5<\/code>, \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c <code>motor<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0440\u043e\u0434\u043d\u043e\u0439 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043e\u0439, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0439 \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0441\u043b\u043e\u0432 <code>async<\/code> \u0438 <code>await<\/code> \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441:  <\/p>\n<pre><code class=\"python\">async def f():     await collection.insert({'_id': 1}) <\/code><\/pre>\n<p>  \u041a\u0443\u0440\u0441\u043e\u0440 \u0438\u0437 <a href=\"http:\/\/motor.readthedocs.org\/en\/latest\/api\/motor_collection.html#motor.motor_tornado.MotorCollection.find\">MotorCollection.find<\/a>, <a href=\"http:\/\/motor.readthedocs.org\/en\/latest\/api\/motor_collection.html#motor.motor_tornado.MotorCollection.aggregate\">MotorCollection.aggregate<\/a>, \u0438\u043b\u0438 <a href=\"http:\/\/motor.readthedocs.org\/en\/latest\/api\/gridfs.html#motor.motor_tornado.MotorGridFS.find\">MotorGridFS.find<\/a> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u0438 \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u0432 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u0445 (coroutines) \u0441 <code>async for<\/code>:  <\/p>\n<pre><code class=\"python\">async def f():     async for doc in collection.find():         print(doc) <\/code><\/pre>\n<p>  \u041d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e? \u0414\u043b\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0438\u0437 10 000 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u044d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0441\u044f \u0437\u0430 0.14 \u0441\u0435\u043a\u0443\u043d\u0434\u044b (14 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434).  <\/p>\n<pre><code class=\"python\"># Motor 0.5 with Tornado. @gen.coroutine def f():     cursor = collection.find()     while (yield cursor.fetch_next):         doc = cursor.next_object()         print(doc) <\/code><\/pre>\n<p>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043c\u0435\u043d\u0435\u043d\u044b <code>gen.coroutine<\/code> \u0438 <code>yield<\/code> \u043d\u0430 <code>async<\/code> \u0438 <code>await<\/code> \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u043e\u0436\u0435.  <\/p>\n<pre><code class=\"python\"># Motor 0.5 with Tornado, using async and await. async def f():     cursor = collection.find()     while (await cursor.fetch_next):         doc = cursor.next_object()         print(doc) <\/code><\/pre>\n<p>  \u041d\u043e \u0441 <code>async for<\/code> \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 0.04 \u0441\u0435\u043a\u0443\u043d\u0434\u044b (4 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u044b), \u0442\u043e \u0435\u0441\u0442\u044c \u0432 \u0442\u0440\u0438 \u0440\u0430\u0437\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435.  <\/p>\n<pre><code class=\"python\"># Motor 0.5 with Tornado, using async for. async def f():     cursor = collection.find()     async for doc in cursor:         print(doc) <\/code><\/pre>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e, MotorCursor \u0432 <a href=\"http:\/\/motor.readthedocs.org\/en\/latest\/api\/motor_cursor.html#motor.motor_tornado.MotorCursor.to_list\">to_list<\/a> \u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0438\u0433\u0440\u0430\u0435\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0440\u043e\u043b\u044c:  <\/p>\n<pre><code class=\"python\"># Motor 0.5 with Tornado, using to_list. async def f():     cursor = collection.find()     docs = await cursor.to_list(length=100)     while docs:         for doc in docs:             print(doc)         docs = await cursor.to_list(length=100) <\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0441 <code>to_list<\/code> \u0432 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0447\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435, \u043d\u043e \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435 \u0442\u0430\u043a \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0447\u0430\u043d\u043a\u0430. \u042f \u0434\u0443\u043c\u0430\u044e, \u0447\u0442\u043e <code>async for<\/code> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u0442\u0438\u043b\u044c\u043d\u043e \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432.<\/p>\n<p>  \u0411\u0435\u0442\u0430 \u0432\u0435\u0440\u0441\u0438\u0438 \u0440\u0435\u043b\u0438\u0437\u043e\u0432 <code>motor<\/code> \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430, \u043d\u043e \u0432 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u043f\u043e-\u0434\u0440\u0443\u0433\u043e\u043c\u0443. \u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f <code>asyncio<\/code> \u0432 <code>motor<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u043e\u0432\u043e\u0439. \u0418 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e \u043f\u043e\u0432\u0441\u0435\u043c\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u044f\u0434\u0440\u0430 <code>motor<\/code>, \u0438 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u043d\u0438\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 <code>tornado<\/code>, \u0431\u044b\u043b\u0430 \u0432\u044b\u043f\u0443\u0449\u0435\u043d\u0430 \u0431\u0435\u0442\u0430-\u0432\u0435\u0440\u0441\u0438\u044f \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0443\u043f\u0443\u0449\u0435\u043d\u0438\u044f.<\/p>\n<p>  <i>P.S. \u041f\u0440\u043e\u0441\u044c\u0431\u0430 \u043e \u0433\u0440\u0430\u043c\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0448\u0438\u0431\u043a\u0430\u0445 \u0438 \u043e\u0448\u0438\u0431\u043a\u0430\u0445 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430 \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u043b\u0438\u0447\u043a\u0443. <\/i>       <\/p>\n<div class=\"clear\"><\/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\/270709\/\"> http:\/\/habrahabr.ru\/post\/270709\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       <i>\u042d\u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u043e\u043b\u044c\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043e\u0431 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043d\u043e\u0432\u0448\u0435\u0441\u0442\u0432\u0430\u0445 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u0430 \u0434\u043b\u044f <code>mongodb<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u0432 <code>tornado<\/code>. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043c\u043e\u0442\u0438\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0441\u043b\u0443\u0436\u0438\u043b \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0430 \u2014 \u043d\u043e\u0432\u0448\u0435\u0441\u0442\u0432\u0430, \u043f\u043e\u044f\u0432\u0438\u0432\u0448\u0438\u0435\u0441\u044f \u0432 \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 <code>asyncio<\/code>, <code>async<\/code>, <code>await<\/code> \u0438 <code>Python 3.5<\/code>. \u0421\u0430\u043c\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0435 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u0448\u0435\u0441\u0442\u0432, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 <code>MongoDB<\/code>. <br \/>  <\/i><\/p>\n<p>  <a href=\"#0\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/a><br \/>  <a href=\"#1\"> asyncio<\/a><br \/>  <a href=\"#2\"> aggregate<\/a><br \/>  <a href=\"#3\"> Python 3.5 <\/a><br \/>  <a href=\"#4\"> async and await<\/a><\/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-268225","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/268225","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=268225"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/268225\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=268225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=268225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=268225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}