MongoEngine and working with "UserWarning: MongoClient open before fork. Create MongoClient with connection = False or create client after forking"
I am using Celery and MongoEngine as part of my Django application with.
I get this warning when @shared_task celery accesses the mongodb database via the mongoengine model classes:
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
It clearly has something to do with multiprocessing and pyMongo, which are based on mongoengine.
My question is:
What is the best strategy to avoid this mongoengine problem?
Note that I am connecting to mongodb with mongoengine in settings.py
:
mongoengine.connect('my_mongo_database_name', alias='default')
source to share
After a little searching on the internet, I found out that it is possible to pass additional arguments to a function mongoengine.connect
, additional arguments will be passed to base classes and functions PyMongo
.
So, I just edited the call mongoengine.connect()
to the following:
mongoengine.connect('my_mongo_database_name', alias='default', connect=False)
And the warning stopped appearing. However, I'm not sure if this is the best way to deal with the warning. If you have a better answer, please post it and I will gladly go through it and eventually accept it.
source to share