OperationFailure:在MongoEngine/PyMongo中执行线程处理时发生数据库错误 [英] OperationFailure: database error when threading in MongoEngine/PyMongo

查看:80
本文介绍了OperationFailure:在MongoEngine/PyMongo中执行线程处理时发生数据库错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,可以从网站读取数据,进行处理,然后将其加载到MongoDB中.当我在不使用线程的情况下运行此程序时,它工作正常,但是一旦设置了仅调用此功能的celery任务,我经常会收到以下错误消息:"OperationFailure:数据库错误:未经授权的db:dbname锁定类型:-1"

I have a function that will read data from a website, process it, and then load it into MongoDB. When I run this without threading it works fine but as soon as I set up celery tasks that just call this one function I frequently get the following error: "OperationFailure: database error: unauthorized db:dbname lock type:-1"

这有点奇怪,因为如果我在多个终端上运行非芹菜版本,则根本不会收到此错误.

It's somewhat odd because if I run the non-celery version on multiple terminals, I do not get this error at all.

我怀疑这与没有到Mongo的开放连接有关,尽管在我的代码中,我在每次Mongo调用之前就打开一个.

I suspect it has something to do with there not being an open connection to Mongo although in my code I'm opening one up right before every Mongo call.

确切的例外如下:

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即将被调用之前已打开连接.

Sorry for the formatting but if you look at the line that starts with mmo.Moment there's a connection being opened right before that's called.

进行一些研究,似乎与PyMongo中处理线程的方式有关-

Doing a bit of research it looks as if it has something to do with the way threading is handled in PyMongo - http://api.mongodb.org/python/1.5.1/faq.html#how-does-connection-pooling-work-in-pymongo - I may need to start closing the connections but I'd expect MongoEngine to be doing this..

推荐答案

这可能是由于在启动新连接并在MongoDB上使用auth时未调用db.authenticate()的事实.

This is likely due to the fact that you are not calling db.authenticate() when you start the new connection and are using auth on MongoDB.

关于线程的关闭,我建议您确保使用连接池并让驱动程序管理池(手动调用close()或类似方法可能会导致很多麻烦).

Regarding the closing of threads, I would recommend making sure you are using connection pooling and letting the driver manage the pools (calling close() or similar manually can lead to a lot of pain).

有关更多信息,请参见该说明在pymongo文档中有关在多线程环境中使用authenticate()的信息.

For more info see the note in the pymongo documentation about using authenticate() in a multi-threaded environment.

这篇关于OperationFailure:在MongoEngine/PyMongo中执行线程处理时发生数据库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆