2016-03-21 51 views
-1

我有一個巨大的龍捲風應用程序是以阻塞的方式編寫的。我試圖將我的數據庫調用轉換爲運行異步。我有很多問題。爲什麼mongodb電機無法正確解析數據?

我將mongo調用保存在名爲lib的頂級文件夾中,並保存在應用程序文件夾中我保留了所有視圖。

我越來越

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

我有大量的代碼I必須翻譯得到這個異步正常工作,我非常感謝任何幫助。謝謝。

回答

1

要從get_chains_updated返回對象列表,您必須是列表(Python 3)或raise gen.Return(slugs)(所有Python版本)的return slugs。欲瞭解更多信息,請參閱Refactoring Tornado Coroutines