MongoDB M0集群,Python多线程

时间:2019-10-17 18:07:02

标签: python mongodb multithreading pymongo

我有一个Python应用程序,该应用程序可以通过Web抓取并利用mongo数据库维护记录。在执行的某些时候,会有大量的数据库请求进出。发生这种情况时,服务器会强制关闭我的请求,并在群集中给出以下错误:

Connections % of configured limit has gone above 80

将pymongo与线程一起使用的最佳实践是什么?我想像其他DMBS的mongodb会自动处理并发请求的调度。我是否只需要建​​立一个本地群集或将当前的群集升级到更多连接?

1 个答案:

答案 0 :(得分:0)

这种情况下的典型活动是创建输入队列.Queue并在其中放入任务,并创建多个工作程序以从Queue接收任务。如果您需要限制可以同时访问资源的工作人员数量,请使用threading.Semaphore或threading.Lock希望答案对您有所帮助,请随时提出问题。

import threading as thr
from queue import Queue


def work(input_q):
    """the function take task from input_q and print or return with some code changes (if you want)"""
    while True:
        item = input_q.get()
        if item == "STOP":
            break

        # else do some work here
        print("some result")


if __name__ == "__main__":
    input_q = Queue()
    urls = [...]
    threads_number = 8 # experiment with the number of workers
    workers = [thr.Thread(target=work, args=(input_q,),) for i in range(threads_number)]
    # start workers here
    for w in workers:
        w.start

    # start delivering tasks to workers 
    for task in urls:
        input_q.put(task)

    # "poison pillow" for all workers to stop them:

    for i in range(threads_number):
        input_q.put("STOP")

    # join all workers to main thread here:

    for w in workers:
        w.join

    # show that main thread can continue

    print("Job is done.")