python-motor: asyncio driver seems not working

197 views
Skip to first unread message

imbolc

unread,
Jul 15, 2015, 4:30:04 PM7/15/15
to mongod...@googlegroups.com
import asyncio
from motor.motor_asyncio import AsyncIOMotorClient


db = AsyncIOMotorClient().client.motor_asyncio


@asyncio.coroutine
def async_func(loop):
    yield from db.test.remove()
    yield from db.test.insert({'foo': 111})

    x = yield from db.test.find_one()
    print(x)

    yield from db.test.find()


loop = asyncio.get_event_loop()
loop.run_until_complete(async_func(loop))

Causes the exception:

{'foo': 111, '_id': ObjectId('55a641f7444bcb19b0e18a7e')}
Traceback (most recent call last):
  File "test_motor.py", line 20, in <module>
    loop.run_until_complete(async_func(loop))
  File "/home/imbolc/.pyenv/versions/3.4.3/lib/python3.4/asyncio/base_events.py", line 316, in run_until_complete
    return future.result()
  File "/home/imbolc/.pyenv/versions/3.4.3/lib/python3.4/asyncio/futures.py", line 275, in result
    raise self._exception
  File "/home/imbolc/.pyenv/versions/3.4.3/lib/python3.4/asyncio/tasks.py", line 234, in _step
    result = coro.throw(exc)
  File "test_motor.py", line 16, in async_func
    yield from db.test.find()
RuntimeError: Task got bad yield: MotorCursor(<pymongo.cursor.Cursor object at 0x7f4dde7cd9e8>)
Exception ignored in: 
shell returned 1

A. Jesse Jiryu Davis

unread,
Jul 16, 2015, 12:29:24 PM7/16/15
to mongod...@googlegroups.com
Hi, Motor's asyncio support is in development now, please don't rely on it.

However, in this specific case, "find" does work. Refer to this test case for proper usage of "find":


Correct example:

@asyncio.coroutine
def async_func(loop):
    cursor = db.test.find()
    while (yield from cursor.fetch_next):
        doc = cursor.next_object()

imbolc

unread,
Jul 18, 2015, 6:24:12 AM7/18/15
to mongod...@googlegroups.com
Thank you. Is it final find api? Is there any reasons don't use obvious syntax that i suggested above?

A. Jesse Jiryu Davis

unread,
Jul 18, 2015, 11:39:12 AM7/18/15
to mongod...@googlegroups.com
Yes it's final. The reason "find()" doesn't return a list of all documents in the result set is that the list might be very large, and hurt your application's responsiveness and memory usage. So I emphasize APIs that limit the amount of time and memory your application spends retrieving each batch of documents.

If you're certain the list won't be large, use "yield collection.find().to_list(length=None)" to get all documents in one call:


But if your collection is large and you make a mistake in your query, you'll buffer a large number of documents in RAM. Use wisely. The examples in the tutorial all show safer methods for getting lists of documents.

(Replace "yield" with "yield from" and make other adjustments for asyncio, of course. The docs still only cover Tornado.)

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/k7qmKR5x7-8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/dd19269a-c985-4bb2-97de-72350c0d25bb%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Imbolc

unread,
Jul 18, 2015, 12:13:53 PM7/18/15
to mongod...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages