我有一个使用Stackless Python和stacklesssocket.py的项目。我最近决定将Couchbase添加到我的项目服务器作为我的数据库后端。我是按照(Couchbase site)的说明做到的。然而,它似乎与我的项目不兼容,并且进一步调查似乎与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')
我正在使用stackless python 2.6.5。我可以使用吗?或者Couchbase模块是否简单且与stacklesssocket完全不兼容?我真的想继续使用stackless python和stacklesssocket,因为我的项目是一个具有许多并发连接的网络服务器。我很感激人们的想法。