OperationFailure:在MongoEngine / PyMongo中进行线程处理时出现数据库错误

时间:2012-03-25 04:21:10

标签: django mongodb celery pymongo mongoengine

我有一个函数,它将从网站读取数据,处理它,然后将其加载到MongoDB中。当我在没有线程的情况下运行它时它工作正常但是一旦我设置芹菜任务只调用这个函数我经常得到以下错误:“OperationFailure:数据库错误:未经授权的db:dbname锁类型:-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.Moment开头的行,就会在调用之前打开一个连接。

进行一些研究看起来好像它与PyMongo中处理线程的方式有关 - http://api.mongodb.org/python/1.5.1/faq.html#how-does-connection-pooling-work-in-pymongo - 我可能需要开始关闭连接但我希望MongoEngine这样做..

1 个答案:

答案 0 :(得分:2)

这可能是因为当您启动新连接并在MongoDB上使用auth时,您没有调用db.authenticate()。

关于线程关闭,我建议确保使用连接池并让驱动程序管理池(手动调用close()或类似操作会导致很多痛苦)。

有关详细信息,请参阅pymongo文档中有关在多线程环境中使用authenticate()的the note