如果我有一个文档 ID 列表,并且想从 firestore 中删除它们,最有效的方法是什么?我目前正在列表上使用循环:
document_ids = ["xyz123", "abc0987", "tvu765", ...] # could be up to 30 IDs
for id in document_ids:
database.collection("documents").document(id).delete()
这是通过从前端到 Flask 路由的 AJAX 调用来完成的,完成后它会发回响应,但是当有 20 多个 id 时,可能需要几秒钟才能完成该过程。
有没有办法在这里说,从这个集合中删除这些?
最佳答案
也许您可以使用多处理
来加速 io 绑定(bind)操作。
使用多重处理来加速
import multiprocessing
import time
from google.cloud import firestore
document_ids = ["xyz123", "abc0987", "tvu765", ...] # could be up to 30 IDs
def delete_doc(doc_id):
database = firestore.Client()
database.collection("documents").document(doc_id).delete()
t_start = time.perf_counter()
processes = list()
for id in document_ids:
p = multiprocessing.Process(target=delete_doc, args=[id,])
p.start()
processes.append(p)
for p in processes:
p.join()
t_finish = time.perf_counter()
print("Total Elapsed Time: {} s".format(round(t_finish - t_start, 3)))
关于python - 如何使用 Python 从 Firestore 批量删除特定文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58072492/