2015-05-04 66 views
0

我有一个为应用程序创建线程数量的python应用程序。每个线程连接到mongodb并检索数据。允许连接到mongodb的数量是200我正在使用信号量照顾。一旦mongo查询工作完成,每个线程关闭mongodb连接。但是在执行这个应用程序时,我得到的所有线程的错误。错误是:在pymongo中检索结果时出错

Traceback (most recent call last): 
    File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner 
    self.run() 
    File "C:\Python34\lib\threading.py", line 869, in run 
    self._target(*self._args, **self._kwargs) 
    File "C:/path/pytest/under_construction/testAlgo.py", line 95, in sample_thread 
    status=monObj.process_status(list_value1,list_value2,5,120,120) 
    File "C:\path\pytest\under_construction\mongo_lib.py", line 153, in process_status 
    result=self.mongo_result('Submission','find',q={}) 
    File "C:\path\pytest\under_construction\mongo_lib.py", line 53, in mongo_result 
    result=list(_query[query_type.lower()](query_string[keys])) 
    File "C:\Python34\lib\site-packages\pymongo\cursor.py", line 1076, in __next__ 
    if len(self.__data) or self._refresh(): 
    File "C:\Python34\lib\site-packages\pymongo\cursor.py", line 1037, in _refresh 
    limit, self.__id)) 
    File "C:\Python34\lib\site-packages\pymongo\cursor.py", line 933, in __send_message 
    res = client._send_message_with_response(message, **kwargs) 
    File "C:\Python34\lib\site-packages\pymongo\mongo_client.py", line 1205, in _send_message_with_response 
    response = self.__send_and_receive(message, sock_info) 
    File "C:\Python34\lib\site-packages\pymongo\mongo_client.py", line 1182, in __send_and_receive 
    return self.__receive_message_on_socket(1, request_id, sock_info) 
    File "C:\Python34\lib\site-packages\pymongo\mongo_client.py", line 1174, in __receive_message_on_socket 
    return self.__receive_data_on_socket(length - 16, sock_info) 
    File "C:\Python34\lib\site-packages\pymongo\mongo_client.py", line 1153, in __receive_data_on_socket 
    chunk = sock_info.sock.recv(length) 
MemoryError 

代码创建蒙戈连接

client=MongoClient(mc_name,port) 

我就在想,这个错误是由于在运行我的应用程序的机器的一个端口积累的所有线程的结果吗?

回答

1

MongoClient是一个线程安全的连接池,因此您应该创建一个由所有工作线程共享的单个实例,而不是让每个线程创建它自己的线程。

连接池大小默认为100,但如果您想使其更大,可以使用maxPoolSize参数来完成此操作(例如maxPoolSize=200)。

+0

当我们给MongoClient(mongo_server,port)时,我们实际上是打开连接到mongodb,ri8?所以,查询这个连接时,许多线程都不能使用它。请澄清。我的困惑在于此。 – pslover

+0

我将相同的连接对象传递给所有线程(按照你的建议),但仍然失败给出相同的错误。 – pslover