python - 如何使用 Python 从 Firestore 批量删除特定文档

标签 python io google-cloud-firestore multiprocessing

如果我有一个文档 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)操作。

使用多重处理来加速

Refer: YouTube Video

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/

相关文章:

python - 为什么读取一个字节比从文件中读取 2、3、4……字节慢 20 倍?

javascript - Firestore 总是进行 3 次 api 调用,其中一次需要 60 秒

C 倒回来自命令输出的 FILE 指针

c - 在范围内定义用户输入

python - 在 Python 中使用字典替换子字符串

python - 如何计算平均 TPR、TNR、FPR、FNR - 多类分类

android - 单击按钮后,从recyclerview中删除项目-Kotlin MVVM Firestore

firebase - Flutter:上传图像未处理的异常:PlatformException(firebase_storage,存储 Uri 不能包含路径元素。,{},null)

python - 是否可以在 leftOuterJoin 上初始化一个空的默认值?

python - drop_sel 不接受切片