MongoDB M0集群,python多线程

MongoDB M0集群,python多线程,python,mongodb,multithreading,pymongo,Python,Mongodb,Multithreading,Pymongo,我有一个python应用程序,它可以通过web刮取并利用mongo数据库来维护记录。在执行过程中的某些时候,会有大量的db请求进出。发生这种情况时,服务器将强制关闭我的请求,并在群集中出现以下错误: Connections%的配置限制已超过80 在线程中使用pymongo的最佳实践是什么?我想和其他DMB的mongodb一样,mongodb会自动处理并发请求的调度。我需要创建一个本地群集还是将当前的一个连接升级到多个连接?这种情况下的典型活动是创建输入队列。队列并将任务放入其中,创建多个工作进程

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

Connections%的配置限制已超过80


在线程中使用pymongo的最佳实践是什么?我想和其他DMB的mongodb一样,mongodb会自动处理并发请求的调度。我需要创建一个本地群集还是将当前的一个连接升级到多个连接?

这种情况下的典型活动是创建输入队列。队列并将任务放入其中,创建多个工作进程以从队列中获取任务。如果您需要限制可以同时使用线程、信号灯或线程锁访问资源的工作人员数量,希望答案对您有所帮助,请随时提问

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.")

这种情况下的典型活动是创建input queue.queue并将任务放入其中,然后创建几个worker从队列中提取任务。如果您需要限制可以同时使用线程、信号灯或线程锁访问资源的工作人员数量,希望答案对您有所帮助,请随时提问

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.")

我试图通过在客户端初始化中添加max pool size关键字arg来解决这个问题,但没有成功。我希望能够做到这一点,而不必为传入请求实现自己的锁。我可以手动解决,但这是愚蠢的,因为我认为我应该能够完成这个小任务。另外,为了进一步说明,在python中,我得到了以下错误:
indexerror:deque为空
我试图通过在客户端初始化中添加max-pool-size关键字arg来解决这个问题,但没有成功。我希望能够做到这一点,而不必为传入请求实现自己的锁。我可以手动解决,但这是愚蠢的,因为我认为我应该能够完成这个小任务。另外,为了进一步说明,在python中,我得到了以下错误:
indexer:deque为空
谢谢您,我甚至没有想到将线程池到工作组中!谢谢你,我甚至没有想过把线程集中到工作组中!