-1

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

我将 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 lib 时的样子

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)))

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

4

1 回答 1

1

要从 中返回对象列表get_chains_updated,您必须是return slugs列表(Python 3)或raise gen.Return(slugs)(所有 Python 版本)。有关详细信息,请参阅重构 Tornado 协程

于 2016-03-21T19:36:36.137 回答