使用Python Cassandra驱动程序输出多个连接错误

问题描述 投票:2回答:1

我使用Datastax提供的Python Cassandra驱动程序连接到单个节点Cassandra实例。我的Python代码生成多个进程(使用多处理模块),每个进程都打开一个与该节点的连接,并在退出时关闭它。

这是我观察到的行为:当产生的进程数量较少(比如说~30)时,我的代码运行得很完美。但是有了更高的数字我会看到这样的错误(可能并不奇怪):

File "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 755, in connect
self.control_connection.connect()
File "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 1868, in connect
self._set_new_connection(self._reconnect_internal())
File "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 1903, in _reconnect_internal

raise NoHostAvailable("Unable to connect to any servers", errors)
NoHostAvailable: ('Unable to connect to any servers', {'127.0.0.1': error(99, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Cannot assign requested address")})

显然,主机无法接受新连接。这看起来应该由驱动程序或Cassandra处理 - 通过让新的连接请求排队并在它释放时授予它们。

我该如何强加这种行为?

python-2.7 cassandra-2.0 datastax
1个回答
2
投票

“无法分配请求的地址”可以表明您的本地端口已用完。这不取决于驱动程序 - 这是一个系统配置问题。 Here is a good article about the problem(它指的是MySQL,但问题是一样的)。请注意,TIME_WAIT状态下的连接占用本地端口,并且可以延伸到单个程序运行之外。

本文讨论了多种解决方案,包括扩展端口范围,侦听多个IP地址或更改应用程序连接行为。我会考虑应用程序行为,并建议运行更少的进程。根据您尝试通过多处理来克服的问题,您可能最好使用(进程计数)<=(机器核心)(这是multiprocessing.Pool的默认行为)。

© www.soinside.com 2019 - 2024. All rights reserved.