MongoEngine并处理"UserWarning:在分叉之前打开MongoClient.使用connect = False创建MongoClient,或者在分叉之后创建客户端. [英] MongoEngine and dealing with "UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking"
问题描述
我将Celery和MongoEngine用作Django应用程序的一部分.
I am using Celery and MongoEngine as part of my Django App with.
当celery @shared_task通过mongoengine模型类访问mongodb数据库时,我收到此警告:
I am getting this warning, when a celery @shared_task accesses the mongodb database via 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
它显然与mongoengine所基于的多处理和pyMongo有关.
It clearly has something to do with multiprocessing and pyMongo that is that mongoengine is based on.
我的问题是:
使用mongoengine避免此问题的最佳策略是什么?
What is the best strategy to avoid this issue with mongoengine?
请注意,我正在使用settings.py
中的mongoengine连接到mongodb:
Please note that i am connecting to the mongodb with mongoengine in the settings.py
:
mongoengine.connect('my_mongo_database_name', alias='default')
推荐答案
在线搜索了一下之后,我发现可以将附加参数传递给mongoengine.connect
函数,附加参数将传递给底层PyMongo
类和功能.
After searching a little bit online, i found out that it is possible to pass additional arguments to the mongoengine.connect
function the additional arguments will be passed to the underlying PyMongo
classes & functions.
所以我只是将mongoengine.connect()
调用编辑为以下内容:
So i simply edited the mongoengine.connect()
call to the following:
mongoengine.connect('my_mongo_database_name', alias='default', connect=False)
并且警告停止出现.尽管如此,我不确定这是否是处理警告的最佳方法.如果您有更好的答案,请发布它,我会很乐意对其进行测试并最终接受.
And the warning stopped appearing. None the less i am not sure this is the best way to deal with the warning. If you have a better answer please post it and i will gladly test it and eventually accept it.
这篇关于MongoEngine并处理"UserWarning:在分叉之前打开MongoClient.使用connect = False创建MongoClient,或者在分叉之后创建客户端.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!