【发布时间】:2016-02-23 11:02:00
【问题描述】:
我正在尝试使用多线程 Python 程序将记录插入 Cassandra。我在 3 台机器上同时运行这个程序。有一段时间记录被插入,但后来我遇到了异常。我正在使用 datastax 提供的驱动程序。
cassandra.cluster.NoHostAvailable
我做了一些搜索发现(来源:https://datastax.github.io/python-driver/api/cassandra/cluster.html)
异常 cassandra.cluster.NoHostAvailable
操作时引发 已尝试但所有连接都忙、已失效、已关闭或 使用时出现错误。
我的问题是:
1. 如果与 Cassandra 有太多联系,这是一个正常的例外吗?
2. 在我想创建许多与 cassandra 的连接/会话的情况下,我将如何解决这个问题。 (我知道创建太多会话是不可取的,它会影响服务器性能,因为每个会话都会消耗少量内存)
下面是代码片段。
cluster = Cluster(['192.168.1.21'])
session = cluster.connect('myNameSpace')
def insertInToCassandra(catRange):
for x in catRange:
//function to insert records into Cassandra table
ProductRange = [
range(900,920),
range(921,940),
range(941,960),
range(961,980),
range(981,1000)
]
# Make the Pool of workers
pool = ThreadPool(20)
# Open the urls in their own threads
# and return the results
results = pool.map(insertInToCassandra, ProductRange)
#close the pool and wait for the work to finish
pool.close()
pool.join()
【问题讨论】:
标签: python cassandra cassandra-2.0