Python中带有多处理池的PyMongo

2024-10-01 22:34:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个困扰了我好几个星期的问题

def doSomething(item):
   client = MongoClient(DB_CONNECTION_STRING, maxPoolSize=100000, connect=False)
   dbclient = client.mydb

   try:
       getAWebpage()
   except MyException as e:
      client.close()
      # I know that the recursion here could cause problems, but this is not the point
      doSomething(item)
   else:
      # do something with the web page and save in the DB
      dbclient.collection.insertone({...})
      ...
      client.close()

def singleTask(item):
   doSomething(item)
   time.sleep(0.3)

def startTask(list, threads=10):
   pool = ThreadPool(threads)
   results = pool.map(singleTask, list)
   pool.close()
   pool.join()

   return results

我在MongoDB日志中看到,它打开了很多连接,但没有关闭它们,并且在某个时刻MongoDB停止接受连接,导致所有连接崩溃

这个代码有什么问题?我真的不明白。 我知道递归可能会导致整个代码出现问题,但我首先尝试解决这个问题

任何帮助都将不胜感激!多谢各位


Tags: the代码clientclosedbmongodbdefitem

热门问题