Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoClient opened before fork. Create MongoClient

When I start some of my services, it reports such warnings and the services stop:

/usr/lib64/python2.6/site-packages/pymongo/topology.py:75: 
UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, 
or create client after forking. See PyMongo's documentation for details: 
http://api.mongodb.org/python/current/faq.html#using-pymongo-with-multiprocessing>
"MongoClient opened before fork. Create MongoClient "

However, the MongoClient has been using the parameter connect=False, as you can review the code below:

client = MongoClient(host, port, connect=False)

It is still not working. By the way, I have upgraded my pymongo version to 3.4.0. Can someone give me some suggestions?

Cheers, Kai

like image 497
KOP Lee Avatar asked Sep 27 '17 07:09

KOP Lee


1 Answers

If you use the MongoClient for any operation that contacts the MongoDB server, then the MongoClient must create connections and background threads. Once this has happened it is no longer safe to use it in a forked subprocess. For example, this is unsafe:

client = MongoClient(connect=False)
client.admin.command('ping')  # The client now connects.
if not os.fork():
    client.admin.command('ping')  # This will print the warning.

Make sure that you aren't doing anything with the client before the fork that causes it to connect.

Better yet, don't create the client before forking at all. Create your client after the fork, in the subprocess.

like image 62
A. Jesse Jiryu Davis Avatar answered Oct 14 '22 21:10

A. Jesse Jiryu Davis