我有一个函数,它将从网站读取数据,进行处理,然后将其加载到MongoDB中。当我在没有线程的情况下运行它时,它工作得很好,但一旦我设置了只调用这一个函数的芹菜任务,我就会经常收到以下错误:"OperationFailure:database error:unauthorized db:dbname lock type:-1"
这有点奇怪,因为如果我在多个终端上运行非芹菜版本,我根本不会得到这个错误。
我怀疑这与没有打开到Mongo的连接有关,尽管在我的代码中,我在每次Mongo调用之前都会打开一个连接。
确切的例外情况如下:
Task twitter[a974bfcc-d6ca-4baf-b36f-cae9143ce2d9] raised exception: OperationFailure(u'database error: unauthorized db:data lock type:-1 client:68.193.49.9',)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/celery/execute/trace.py", line 36, in trace
return cls(states.SUCCESS, retval=fun(*args, **kwargs))
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/celery/app/task/__init__.py", line 232, in __call__
return self.run(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/celery/app/__init__.py", line 172, in run
return fun(*args, **kwargs)
File "/djangoblog/network/tasks.py", line 40, in twitter
n_twitter.GetTweetsTwitter(user)
File "/djangoblog/network/twitter.py", line 255, in GetTweetsTwitter
id = SaveTweet(user, network, tweet)
File "/djangoblog/network/twitter.py", line 150, in SaveTweet
if mmo.Moment.objects(user=user.id,source_id=id,network=network.id).count() == 0:
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mongoengine/queryset.py", line 933, in count
return self._cursor.count(with_limit_and_skip=True)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mongoengine/queryset.py", line 563, in _cursor
self._cursor_obj = self._collection.find(self._query,
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mongoengine/queryset.py", line 493, in _collection
if self._collection_obj.name not in db.collection_names():
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pymongo/database.py", line 361, in collection_names
names = [r["name"] for r in results]
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pymongo/cursor.py", line 703, in next
if len(self.__data) or self._refresh():
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pymongo/cursor.py", line 666, in _refresh
self.__uuid_subtype))
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pymongo/cursor.py", line 628, in __send_message self.__tz_aware)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pymongo/helpers.py", line 101, in _unpack_response error_object["$err"])
OperationFailure: database error: unauthorized db:data lock type:-1 client:68.193.49.9
很抱歉格式化,但如果你看看以mmo开头的行。在调用之前,有一个连接正在打开。
做一点研究,它看起来似乎与PyMongo中处理线程的方式有关——http://api.mongodb.org/python/1.5.1/faq.html#how-连接池在pymongo中工作吗?我可能需要开始关闭连接,但我希望MongoEngine会这样做。。
这可能是因为当您启动新连接并在MongoDB上使用auth时,您没有调用db.authenticate()。
关于线程的关闭,我建议您确保使用连接池,并让驱动程序管理池(手动调用close()或类似操作可能会带来很多痛苦)。
有关更多信息,请参阅pymongo文档中关于在多线程环境中使用authenticate()的说明。