在 Python 中删除 async/await 的 Firestore 集合
Deleting a Firestore collection with async/await in Python
我正在使用带有 python 的 Firebase Admin SDK,并想删除带有 Async/Await 子句的 Firestore 集合。
official document 表示(非Async/Await)删除应该这样进行:
def delete_collection(col_ref, batch_size):
docs = col_ref.limit(batch_size).stream()
deleted = 0
for doc in docs:
print(f'Deleting doc {doc.id} => {doc.to_dict()}')
doc.reference.delete()
deleted = deleted + 1
if deleted >= batch_size:
return delete_collection(col_ref, batch_size)
但是,这在 Async/Await 版本中效果不佳。如果我把 AsyncCollectionReference
放在 col_ref
中,它会导致这个错误:
TypeError: 'async_generator' object is not iterable
这个问题可以解决吗?或者,Async/Await批量删除是完全错误的想法吗?
谢谢!
尝试在您的代码中添加 async/await 语句:
async def delete_collection(coll_ref, batch_size):
docs = coll_ref.limit(batch_size).stream()
deleted = 0
async for doc in docs:
print(f"Deleting doc {doc.id} => {doc.to_dict()}")
await doc.reference.delete()
deleted = deleted + 1
if deleted >= batch_size:
return delete_collection(coll_ref, batch_size)
我正在使用带有 python 的 Firebase Admin SDK,并想删除带有 Async/Await 子句的 Firestore 集合。
official document 表示(非Async/Await)删除应该这样进行:
def delete_collection(col_ref, batch_size):
docs = col_ref.limit(batch_size).stream()
deleted = 0
for doc in docs:
print(f'Deleting doc {doc.id} => {doc.to_dict()}')
doc.reference.delete()
deleted = deleted + 1
if deleted >= batch_size:
return delete_collection(col_ref, batch_size)
但是,这在 Async/Await 版本中效果不佳。如果我把 AsyncCollectionReference
放在 col_ref
中,它会导致这个错误:
TypeError: 'async_generator' object is not iterable
这个问题可以解决吗?或者,Async/Await批量删除是完全错误的想法吗?
谢谢!
尝试在您的代码中添加 async/await 语句:
async def delete_collection(coll_ref, batch_size):
docs = coll_ref.limit(batch_size).stream()
deleted = 0
async for doc in docs:
print(f"Deleting doc {doc.id} => {doc.to_dict()}")
await doc.reference.delete()
deleted = deleted + 1
if deleted >= batch_size:
return delete_collection(coll_ref, batch_size)