【问题标题】:Why does mongodb motor not resolve the data correctly?为什么 mongodb motor 不能正确解析数据?
【发布时间】:2016-03-21 18:06:06
【问题描述】:

我有一个以阻塞方式编写的大型龙卷风应用程序。我正在尝试将我的数据库调用转换为异步运行。我有很多问题。

我将 mongo 调用保存在名为 lib 的顶级文件夹中,并在 app 文件夹中保存我的所有视图。

我遇到的错误

Traceback (most recent call last):
  File "/Users/marcsantiago/staging_env/lib/python2.7/site-packages/tornado/web.py", line 1445, in _execute
    result = yield result
  File "/Users/marcsantiago/staging_env/lib/python2.7/site-packages/tornado/gen.py", line 1008, in run
    value = future.result()
  File "/Users/marcsantiago/staging_env/lib/python2.7/site-packages/tornado/concurrent.py", line 232, in result
    raise_exc_info(self._exc_info)
  File "/Users/marcsantiago/staging_env/lib/python2.7/site-packages/tornado/gen.py", line 1017, in run
    yielded = self.gen.send(value)
  File "/Users/marcsantiago/pubgears/app/admin.py", line 179, in get
    notes, start_date, stats, last_updated = self.db_data()
  File "/Users/marcsantiago/pubgears/app/admin.py", line 76, in db_data
    while (yield chain_slugs_updated.fetch_next):
AttributeError: 'NoneType' object has no attribute 'fetch_next'

所以在lib文件夹里面我有这个方法。

def get_chains_updated(date):
  slugs = []
  # Chain class can't do aggregate could create a class instance if i want
  cursor = db.chain.aggregate([
    {'$match':{'last_update':{'$gt':date}}}, 
    {'$group':{'_id':{'site':'$site'}, 'total':{'$sum':'$count'}}}
  ])

  while (yield cursor.fetch_next):
    res = yield cursor.next_object()
    slugs.append(res['_id']['site'])

  yield slugs

后来我把这个方法称为我的观点之一

chain_slugs_updated = yield chaindb.get_chains_updated(yesterday)
slugs = []
#for site in chain_slugs_updated:
while (yield chain_slugs_updated.fetch_next):
  site = chain_slugs_updated.next_object()
  slugs.append('<a href="/admin/sites/settings?slug=%s">%s</a>' % (site, site))
notes.append('<strong>%s</strong> chains have been updated in the past 24 hours (%s).' % (chain_slugs_updated.count(), ', '.join(slugs)))

这就是我使用 pymongo 时的样子 库

def get_chains_updated(date):
  slugs = []
  # Chain class can't do aggregate could create a class instance if i want
  results = db.chain.aggregate([
    {'$match':{'last_update':{'$gt':date}}}, 
    {'$group':{'_id':{'site':'$site'}, 'total':{'$sum':'$count'}}}
  ])
  for res in results:
    slugs.append(res['_id']['site'])
  return slugs

查看

chain_slugs_updated = chaindb.get_chains_updated(yesterday)
    slugs = []
    for site in chain_slugs_updated:
      slugs.append('<a href="/admin/sites/settings?slug=%s">%s</a>' % (site, site))
    notes.append('<strong>%s</strong> chains have been updated in the past 24 hours (%s).' % (len(chain_slugs_updated), ', '.join(slugs)))

我必须翻译大量代码才能使异步正常工作,我非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: mongodb tornado tornado-motor


    【解决方案1】:

    要从 get_chains_updated 返回对象列表,您必须要么 return slugs 列表 (Python 3) 要么 raise gen.Return(slugs) (所有 Python 版本)。如需更多信息,请参阅Refactoring Tornado Coroutines

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多