python - Open cassandra connection only once in Django -


i want open 1 single connection cassandra database in django. did not find on topic unfortunately.

at moment got class iniate whenever need query:

class cassconnection():      def __init__(self):         self.auth_prov = plaintextauthprovider(settings.runtime_settings[k.cassandra_user],                                                settings.runtime_settings[k.cassandra_password])         self.cluster = cluster(settings.runtime_settings[k.cassandra_cluster], auth_provider=self.auth_prov)         self.session = self.cluster.connect(keyspace=settings.runtime_settings[k.keyspace_tirereadings])         self.session.row_factory = dict_factory      def get_session(self):         return self.session 

i open new session in other classes every query make by:

    self.con = cassconnection()     self.session = self.con.get_session() 

anyone hint, how keep session open , make accesible via multiple packages?

for "one connection per django process" scheme, basically, want is

  • a connection proxy class (which have) "lazy" connection behaviour (doesn't connect until tries use connection),
  • a module-global instance of class other packages can import, ,
  • a way ensure connection gets closed (which cassandra requires proper operation)

this last point main difficulty none of available options (mainly atexit.register() , __del__(self) method) 101% reliable. implementing __del__(self) on connection proxy might reliable still, beware of circular depencies (http://eli.thegreenplace.net/2009/06/12/safely-using-destructors-in-python might read here).

also note "one single connection per django process" mean connections must totally thread-safe, have many threads per django process (depending on wsgi container configuration).

another solution - if thread-safety issue - might have single connection per request...


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -