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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With