当我以这种方式定义pymongo集合时,线程数将增加2:
import threading
print threading.activeCount()
def db_bigadevs():
from pymongo import MongoClient
con_bigadevs = MongoClient()
return con_bigadevs['bigadevs']
class collections:
print 'A1 %s' % threading.activeCount()
col_webs = db_bigadevs()['webs']
print 'A2 %s' % threading.activeCount()
输出是:
1
A1 1
A2 3
答案 0 :(得分:1)
每个MongoClient都有一个用于定期清理任务的后台线程,以及每个服务器的后台线程,用于监控MongoDB服务器的状态。因此,如果您创建默认的MongoClient,它将连接到localhost上的一个服务器:27017,产生总共两个线程。
A brief explanation is here, where the FAQ says "MongoClient spawns multiple threads to run background tasks such as monitoring connected servers." I wrote a very thorough explanation of PyMongo's use of threads here,主要面向未来的PyMongo维护者。