使用多处理的datastax python驱动程序连接超时

时间:2014-04-28 14:49:29

标签: cassandra multiprocessing

以下示例:

from cassandra.cluster import Cluster
from cassandra.decoder import *
import multiprocessing

def async_query_process(lock, i, process_num):

    if i < process_num:
        lock.acquire()
        cluster = Cluster(['neptune'])
        session = cluster.connect('mercure')
        session.row_factory = tuple_factory 
        lock.release()
        process = multiprocessing.Process(name='process' + str(i+1),
                                        target=async_query_process,
                                        args=(lock, i+1, 2))
    process.start()

lock = multiprocessing.Lock()
process = multiprocessing.Process(name='process' + str(0), target=async_query_process,# cluster_ip_list = self.cluster_ip_list, keyspace=self.keyspace,
                                  args=(lock, 0, 2))
process.start()

给出以下错误:

Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "test_api.py", line 14, in async_query_process
    session = cluster.connect('mercure')
  File "/home/nkou/workProd/local/lib/python2.7/site-packages/cassandra/cluster.py", line 483, in connect
    self.control_connection.connect()
  File "/home/nkou/workProd/local/lib/python2.7/site-packages/cassandra/cluster.py", line 1369, in connect
    self._set_new_connection(self._reconnect_internal())
  File "/home/nkou/workProd/local/lib/python2.7/site-packages/cassandra/cluster.py", line 1405, in _reconnect_internal
    raise NoHostAvailable("Unable to connect to any servers", errors)
NoHostAvailable: ('Unable to connect to any servers', {'neptune': OperationTimedOut('errors=Timed out creating new connection, last_host=None',)})

在datastax python驱动程序doc中提到应该在分叉之后创建集群和会话实例,这就是为什么我必须使用那种嵌套的子进程结构。

我有点被困在这里,也许我会切换回以前工作的cassandra-python驱动程序。

谢谢!

尼科

1 个答案:

答案 0 :(得分:0)

这将起作用:(在启动新进程之前关闭会话)

def async_query_process(lock,i,process_num):

if i < process_num:
    lock.acquire()
    cluster = Cluster(['neptune'])
    session = cluster.connect('mercure')
    session.row_factory = tuple_factory
    session.cluster.shutdown()    # close session here
    session.shutdown() 
    lock.release()
    process = multiprocessing.Process(name='process' + str(i+1),
                                    target=async_query_process,
                                    args=(lock, i+1, 2))
process.start()