rethinkdb / rethinkdb-python

Python driver for RethinkDB
https://rethinkdb.com/api/python/
Apache License 2.0
64 stars 35 forks source link

Python driver: auto reconnect options? #275

Open coffenbacher opened 7 years ago

coffenbacher commented 7 years ago

Is there any better way of getting a connection object that automatically reconnects than below? Closed connections keep throwing exceptions in random places in my code. I really don't care if my connection closed itself for whatever reason, I just want queries to run, so I'm trying to implement something like this 👍

an instance patch

import types

c = r.connect()

def auto_reconnect(self):
    if self._instance is None or not self._instance.is_open():
        self.reconnect()

c.check_open = types.MethodType( auto_reconnect, c )

c.close()

# Succeeds
r.db('simulator').table('watch_list').count().run(c)

or a general monkey patch

from rethinkdb import Connection

def auto_reconnect(self):
    if self._instance is None or not self._instance.is_open():
        self.reconnect()

Connection.check_open = auto_reconnect
4F2E4A2E commented 4 years ago

I am also very interested in a smart reconnect way for the python rethinkdb driver. You script seems to be it, but the import does not know types or Connection.

I am using rethinkdb v2.4.5 pip package.