我用motor在tornado框架下来操作mongodb,按照官方教学文档写的结果正常。
>>> @gen.coroutine
... def do_find_one():
... document = yield db.test_collection.find_one({'i': {'$lt': 2}})
... print document
...
>>> IOLoop.current().run_sync(do_find_one)
{u'i': 0, u'_id': ObjectId('...')}
现在我想实现a.py调用b.py,然后在b.py里面用motor操作mongodb,例如是插入数据,然后返回_id给a.py。我的代码是这样写的:
a.py
from b import testb
from tornado import ioloop
from tornado import gen
class testa(Object):
@gen.coroutine
def printa(self):
tmp = testb()
id = tmp.do_insert()
print id
a = testa()
ioloop.IOLoop.current().run_sync(a.printa)
b.py
from tornado import gen
import motor
client = motor.MotorClient('localhost', 27017)
db = client.testdb
class testb(object):
@gen.coroutine
def do_insert(self):
coll = db.testcoll
yield coll.find_one({'bookname': 'huihuang'})
因为testb加了yield,生成器里面不能用return。我这种写法a.py中print出来的是一个<tornado.concurrent.Future object at 0x7fa83c900e10>
我不知道怎么获得future里面的数据。
哪位大神帮我看看!或者我哪里理解错了