【发布时间】:2021-03-22 00:37:45
【问题描述】:
如何通过 asyncio 运行这个 example?我在启动时遇到的错误:
name col_snapshot is not defined
样本:
async def on_snapshot(col_snapshot, changes, read_time):
logger.debug('Received updates')
col_query = db.collection(u'cities')
query_watch = col_query.on_snapshot(await on_snapshot(col_snapshot, changes, read_time))
import os
import asyncio
from loguru import logger
from google.cloud.firestore import Client
os.environ['GOOGLE_APPLICATION_CREDENTIALS']='E:/Python/listener_firebase/creds.json'
async def callback(col_snapshot, changes, read_time):
logger.debug('Received updates')
for change in changes:
if change.type.name == 'ADDED':
logger.debug(f'New document: {change.document.id}')
await asyncio.sleep(1)
logger.debug('Finished handling the updates')
Client().collection('cities').on_snapshot(callback)
while True:
pass
这对我不起作用:
tch.py:568: RuntimeWarning: coroutine 'callback' was never awaited
self._snapshot_callback(keys, appliedChanges, read_time)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
【问题讨论】:
-
您是否尝试过使用Documentation example 使用
threading? -
on_snapshot不做任何异步操作,因此将其定义为协程确实没有意义,但如果您正在做一些上面没有包含的事情,解决方法是您需要拨打on_snapshot:await on_snaphost(arg1, arg2, arg3)。 -
@dirn 如果我使用
query_watch = col_query.on_snapshot(await on_snapshot(col_snapshot, changes, read_time))然后我得到NameError: name 'col_snapshot' is not defined -
需要先定义变量才能使用。
-
@dirn 但在示例中,它们是隐式传递的。我怎样才能得到它们?
标签: python firebase google-cloud-firestore python-asyncio