1

我刚开始使用 Kazoo,我无法运行一个非常简单的程序:

from kazoo.client import KazooClient
import logging
logging.basicConfig(level=logging.DEBUG)
from kazoo.retry import KazooRetry
_retry = KazooRetry(max_tries=1000, delay=0.5, backoff=2)
zk = KazooClient(hosts='164.55.92.8:2181', logger=logging, timeout=10, connection_retry=_retry, read_only=True)

zk.start()
import time
print('sleeping 5!')
time.sleep(5)
zk.stop()

输出如下所示:

ERROR:root:Unhandled exception in connection loop  
Traceback (most recent call last):  
  File "C:\Python27\lib\site-packages\kazoo-2.0-py2.7.egg\kazoo\protocol\connection.py", line 522, in _connect_attempt  
    [], [], timeout)[0]  
  File "C:\Python27\lib\site-packages\kazoo-2.0-py2.7.egg\kazoo\handlers\threading.py", line 250, in select  
    return select.select(*args, **kwargs)  
error: (10038, 'An operation was attempted on something that is not a socket')  
INFO:root:Zookeeper session lost, state: CLOSED  
Exception in thread Thread-3:  
Traceback (most recent call last):  
  File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner  
    self.run()  
  File "C:\Python27\lib\threading.py", line 504, in run  
    self.__target(*self.__args, **self.__kwargs)  
  File "C:\Python27\lib\site-packages\kazoo-2.0-py2.7.egg\kazoo\protocol\connection.py", line 466, in zk_loop  
    if retry(self._connect_loop, retry) is STOP_CONNECTING:  
  File "C:\Python27\lib\site-packages\kazoo-2.0-py2.7.egg\kazoo\retry.py", line 123, in __call__  
    return func(*args, **kwargs)  
  File "C:\Python27\lib\site-packages\kazoo-2.0-py2.7.egg\kazoo\protocol\connection.py", line 483, in _connect_loop  
    status = self._connect_attempt(host, port, retry)  
  File "C:\Python27\lib\site-packages\kazoo-2.0-py2.7.egg\kazoo\protocol\connection.py", line 522, in _connect_attempt  
    [], [], timeout)[0]  
  File "C:\Python27\lib\site-packages\kazoo-2.0-py2.7.egg\kazoo\handlers\threading.py", line 250, in select  
    return select.select(*args, **kwargs)  
error: (10038, 'An operation was attempted on something that is not a socket')  


Traceback (most recent call last):  
  File "C:\Users\klow\Google Drive\mycode\mykazoo\kazooo.py", line 8, in <module>  
    zk.start()  
  File "C:\Python27\lib\site-packages\kazoo-2.0-py2.7.egg\kazoo\client.py", line 537, in start  
    raise self.handler.timeout_exception("Connection time-out")  
TimeoutError: Connection time-out  
>>>   

我在 Window 7 笔记本电脑上运行它,Zookeeper 服务器在 Linux 机器上运行。看起来 TCP 连接已经建立,之后很快就搞砸了。我稍微研究了一下代码。我可以看到 TCP 连接已在 connection.py:510 (self._connect(host, port)) 处建立,并且 self._socket 已分配给 create_tcp_connection(socket, *args, **kwargs) 返回的对象。但是,connectiom.py:521 处的 self.handle.select() 不喜欢套接字。任何想法?先感谢您!

4

1 回答 1

2

我一直在用类似的设置对此进行调查。Win7 + zookeeper 运行在 linux 机器上。使用 Python 3.4。无论如何,我认为这个电话

s = self.handler.select([self._socket, self._read_pipe],
                                        [], [], timeout)[0]

正在尝试对管道进行选择。根据 python 文档,管道上的选择在 Windows 上不起作用。因此,kazoo 似乎无法在当前架构的 Windows 上运行。我在代码副本中弄乱了配对的套接字,但看起来不太好。

kazoo 不适用于 Windows 客户端这一事实限制了它的实用性......

更新:所以在 kazoo 源代码中进行一些修改可以解决问题。我创建了一个小型实用程序函数来在 Windows 上创建套接字对。此套接字对模拟管道并由 utils 中的 create_pipe 函数返回。客户端中有几个地方调用了 os.read() 和 os.write(),我用 socket.recv 和 socket.send 替换了它们,一切似乎都正常了。

我不太清楚为什么 kazoo 使用管道。它似乎主要用于信号功能,可以用信号做得更好。无论如何,我会检查代码,看看是否可以做出更持久的解决方案。

于 2014-08-17T08:37:33.480 回答