Why does the python Cassandra driver fail when declared as a class field? -
i'm working server process meant connect cassandra database , forward information it. process written class, , goal create single cassandra session class can use send information. however, i'm running problem; when create cassandra session in classes init method, , later try use session in method, following error: errors={}, last_host=<server ip address>
. can around problem creating new cassandra session each time method called, not way go this. so, how can make cassandra session can use consistently throughout class?
this code not work:
from cassandra.cluster import cluster multiprocessing import process class dataprocess(process): def __init__(self): super(dataprocess,self).__init__() # few other irrelevant things ... # set cassandra connection self.cluster = cluster(contact_points=[cassandra_ip]) self.session = self.cluster.connect('some keyspace') print "connected cassandra." def callback(self,ch,method,props,body): prepared_statement = self.session.prepare("some cql statement...") bound_statement = prepared_statement.bind(some values) self.session.execute(bound_statement) output: "connected cassandra." errors={}, last_host=<server ip address>
this code work, it's silly way it:
from cassandra.cluster import cluster multiprocessing import process class dataprocess(process): def __init__(self): super(dataprocess,self).__init__() # few irrelevant things ... def callback(self,ch,method,props,body): # set cassandra connection cluster = cluster(contact_points=[cassandra_ip]) session = cluster.connect('some keyspace') prepared_statement = session.prepare("some cql statement...") bound_statement = prepared_statement.bind(some values) session.execute(bound_statement)
other relevant info:
using python cassandra-driver version 2.5.1
cassandra database version 2.1.8
edit: answer following code solved issue:
from cassandra.cluster import cluster multiprocessing import process class dataprocess(process): def __init__(self): super(dataprocess,self).__init__() self.cluster = none self.session = none # few irrelevant things ... def callback(self,ch,method,props,body): # set cassandra connection cluster = cluster(contact_points=[cassandra_ip]) session = cluster.connect('some keyspace') prepared_statement = session.prepare("some cql statement...") bound_statement = prepared_statement.bind(some values) session.execute(bound_statement) def run(self): self.cluster = cluster(contact_points=[cassandra_ip]) self.session = self.cluster.connect('some keyspace')
are creating cluster , sessions before fork? cause issues seeing. there amazing writeup of how distribute work using pools using python driver here here. looking for.
if it's not please leave more context on how running process, it's tough reproduce without knowing process lifecycle.
Comments
Post a Comment