1. 程式人生 > >Python錯誤集(二)之MongoClient opened before fork警告

Python錯誤集(二)之MongoClient opened before fork警告

今天在使用Python連線MongoDB的過程中日誌出現瞭如下的warning:

UserWarning: MongoClient opened before fork. Create MongoClient only after forking. See PyMongo’s documentation for details: http://api.mongodb.org/python/current/faq.html#is-pymongo-fork-safe
“MongoClient opened before fork. Create MongoClient only “

PyMongo官方文件有如下說明:

PyMongo是現成不安全的,因此MongoClient例項一定不能從父程序複製到子程序中,而應該父程序和子程序建立自己的MongoClient例項.

解決方法:
在例項化MongoClient例項時加上引數connect=False,例如:

from pymongo import MongoClient
client = MongoClient(host='localhost', port=27017, connect=False)