我有一个使用 Stackless Python 和 stacklesssocket.py 的项目。我最近决定将 Couchbase 添加到我的项目中作为我的数据库后端服务器。我按照(Couchbase 站点)的说明进行了此操作。但是它似乎与我的项目不兼容,并且进一步调查似乎与stacklesssocket发生冲突。
显示我的项目的源代码会过于复杂,但我将代码提炼成一个完全重现我得到的错误的案例:
import couchbase
# Monkeypatch in the 'stacklesssocket' module, so we get blocking sockets
# which are Stackless compatible. This example code will avoid any use of
# the Stackless sockets except through normal socket usage.
import stacklesssocket
stacklesssocket.install()
import socket
# connect to a couchbase server
cb = couchbase.Server('localhost:8091', username='username', password='password')
# use default bucket
default_bucket = cb['default']
# fetch a key with a function
print 'test = ' + str(default_bucket.get('test'))
如您所见,我正在添加 stacklesssocket,然后尝试连接到 Couchbase 服务器。症状是下面的堆栈跟踪。我不确定 stacklesssocket 究竟做了什么,但我知道它取代(或增强)了普通 python 套接字的功能。所以看起来 Couchbase 正在使用一个套接字并且它的行为不像预期的那样:
Traceback (most recent call last):
File "/Users/mark/Programming/MarkTestSite/server/src/dbteststackless.py", line 12, in <module>
cb = couchbase.Server('localhost:8091', username='username', password='password')
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/couchbase/client.py", line 50, in __init__
config = ServerHelper.parse_server_config(server_config_uri, username, password)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/couchbase/client.py", line 334, in parse_server_config
response = urlopener.open(uri)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib.py", line 205, in open
return getattr(self, name)(url)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib.py", line 344, in open_http
h.endheaders()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 904, in endheaders
self._send_output()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 776, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 735, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 716, in connect
self.timeout)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 514, in create_connection
raise error, msg
IOError: [Errno socket error] [Errno 61] Connection refused
为了证明 Couchbase 通常对我有用(总是一个很好的完整性检查),这里是有效的代码。我只是删除了导入stacklesssocket的代码:
import couchbase
# connect to a couchbase server
cb = couchbase.Server('localhost:8091', username='username', password='password')
# use default bucket
default_bucket = cb['default']
# fetch a key with a function
print 'test = ' + str(default_bucket.get('test'))
它按预期工作并生成以下输出:
test = (0, 5, 'hello world')
我正在使用无堆栈 python 2.6.5。有没有我可以使用的解决方法?还是 Couchbase 模块只是简单且与 stacklesssocket 不兼容?我真的很想继续使用 stackless python 和 stacklesssocket,因为我的项目是一个具有许多并发连接的网络服务器。我很欣赏人们的任何想法。