同一Python模块中有多个MongoDB数据库触发器



我正在尝试为我的MongoDB实现多个数据库触发器(通过Pymongo连接到我的Python代码(。

我能够成功地实现一个数据库触发器,但无法将其扩展到多个触发器。

单个数据库触发器的代码可以在下面找到:

try:
resume_token = None
pipeline = [{"$match": {"operationType": "insert"}}]
with db.collection.watch(pipeline) as stream:
for insert_change in stream:
print("postprocessing logic goes here")
except pymongo.errors.PyMongoError:
logging.error("Oops")

问题是,一旦实现了单个数据库,后处理代码就会等待接收该集合的传入请求,并且能够在同一模块中包括其他集合监视。

感谢的任何帮助

为每个手表创建单独的函数,并在单独的线程中使用以下内容启动它们:

import queue
import threading
main_q = queue.Queue()
thread1 = threading.Thread(target=function1, args=(main_q, None))
thread1.daemon = True
thread1.start()
thread2 = threading.Thread(target=function2, args=(main_q,))
thread2.daemon = True
thread2.start()

最新更新