Couchbase Python SDK - How to enforce a custom couchbase connection timeout + Retry connections?

I have the following code for connecting to an existing Couchbase bucket and retrieving a document. How can I enforce a custom timeout value (in seconds) for the couchbase connections?

import couchbase
from couchbase.bucket import Bucket
import couchbase.exceptions

cb_bucket = "default"

def fetch_doc(self, key):
         cb = Bucket('couchbase://' + cb_nodes + '/' + cb_bucket)
         doc = cb.get(key)
         return doc
    except couchbase.exceptions.NotFoundError as e:
         print("Key Not Found")
    except couchbase.exceptions.TimeoutError as e:
         print("Couchbase connection timed out")
    except couchbase.exceptions.CouchbaseNetworkError as e:
         print("Couchbase Connection failed")

I am using Python 3, Couchbase 4.1 and Python SDK 2.1 for Couchbase.

I found this - but it does not really state how or where I should set a timeout value.

Another question - How can I make the python client to retry fetching a document when it encounters a timeout and network exception?

Any help with this is greatly appreciated.


There are multiple timeout values you can set. The ones you care about are operation_timeout, config_node_timeout and config_total_timeout. You can read more about settings at:

As for your second question, the point of a timeout is so that control is returned back to the user after the period of time so that the user can either report an error or retry the operation again. You can fetch the document yourself again (though I wouldn’t recommend it) by using a try/except, catching couchbase.exceptions.TimeoutError (check the API ref for the exact name).