pymongo 无法处理多线程

发布于 2025-01-03 03:57:47 字数 586 浏览 1 评论 0原文

下面的代码无论是在python2.6还是python3.2中都不起作用。

测试代码:

import pymongo

class A(Thread):  
        def __init__(self):  
            Thread.__init__(self)  
            self.conn = pymongo.Connection('localhost',30000)  
            self.mongo = self.conn.puppet  
            self.mongo.authenticate('test','123')  
        def run(self):  
            print self.mongo.href.find_one()  

A().start()

异常消息:

 OperationFailure: database error: unauthorized db:puppet lock
   type:-1 client:127.0.0.1  

有什么想法如何解决这个问题吗?

the following code does not work, either in python2.6 or python3.2.

Test Code:

import pymongo

class A(Thread):  
        def __init__(self):  
            Thread.__init__(self)  
            self.conn = pymongo.Connection('localhost',30000)  
            self.mongo = self.conn.puppet  
            self.mongo.authenticate('test','123')  
        def run(self):  
            print self.mongo.href.find_one()  

A().start()

Exception Message:

 OperationFailure: database error: unauthorized db:puppet lock
   type:-1 client:127.0.0.1  

Any ideas how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2025-01-10 03:57:47

这可能是由于 pymongo 中处理身份验证的方式所致,请参阅 API 文档中的注释 - Pymongo 不会缓存线程之间的身份验证凭据,因此每个线程必须单独进行身份验证。

This is probably due to how authentication is handled in pymongo, see the note in the API docs - Pymongo doesn't cache authentication credentials between threads, so each thread must authenticate individually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文