A newer version of this documentation is available.

View Latest

Using asynchronous frameworks

In addition to the simple synchronous API, the Python SDK supports native integration with gevent and twisted asynchronous frameworks for high network performance.
See Reactive asynchronous clients for a general overview on asynchronous/reactive programming and the performance benefits possible when used with Couchbase.

gevent API

gevent is a high performance asynchronous framework. It is very powerful in that it allows for a (mostly) traditional synchronous coding style. It accomplishes this by using coroutines known as greenlets or eventlets.

The gevent API is almost identical to the simple synchronous API, though it requires that a different module be imported to properly integrate with gevent.

from gcouchbase.bucket import Bucket
bucket = Bucket('couchbase://localhost/default')
bucket.upsert('id', {'some':['value']})
print bucket.get('id').value

Twisted API

Twisted is a high performance and very mature Python asynchronous framework.

from twisted.internet import reactor
from txcouchbase.bucket import Bucket
bucket = Bucket('couchbase://localhost/default')

def on_ok(result):
    print "Operation succeeded"
    if hasattr(result, 'value'):
        print "Value is", result.value

def on_err(err):
    print "Operation failed", err

bucket.upsert('id', {'some':['value']}).addBoth(on_ok, on_err)
bucket.get('id').addBoth(on_ok, on_err)