Couchbase Twisted example misleading and wrong?

I’ve been trying out the txcouchbase and reading the documentation on developer.couchbase.com. Here and here the following example is presented:

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)

I thought addBoth took one function as a parameter, and effectively was equivalent to using addCallbacks if you’re using the same callback for both errbacks and callbacks. In order for this code to work I would have thought addCallbacks should be used instead of addBoth ?

Also, can you recommend some readings on the txcouchbase ?

You are correct, addBoth(callback) is equivalent to addCallbacks(callback, callback) - this appears to be an issue with the Python client documentation. In general the Twisted Bucket variant returns Twisted Deferred objects which will behave as described in the twisted documentation (https://twistedmatrix.com/documents/14.0.1/core/howto/defer.html) and is the only significant difference in behaviour from the synchronous bucket. Aside from the (wrong) main Python client documentation you can also checkout the API reference which is generally more correct (http://pythonhosted.org/couchbase/api/txcouchbase.html).

Thank you Will,

I currently am reading the twisted documentation, will see where that gets me :slight_smile:

Is there any blog post or similar that compares Twisted Interface to gevent Interface and perhaps AsyncBucket?

I have been trying Twisted as I believe it would be faster than the “general” python interface, as I’ve never seen Twisted before I tried finding reviews or posts about people’s experience using cb with twisted, or txcouchbase with no luck. I am interested in pros and cons, especially in regards of time (speed) but only reference to timings I found is that Twisted is 3x slower than Async… Given I understood the documentation correctly (“Deferreds result in a 3x performance slowdown”)

I’m not aware of many people using the Twisted Interface, particularly among Enterprise customers. The Twisted Interface will definitely give improved throughput over the synchronous interface and threading but it does have some drawbacks.

The deferred objects are not exactly lightweight and have their own overheads in terms of processing the callbacks (because callbacks can be chained etc.). The txcouchbase.RawBucket interface could be used instead if you need just a single callback / errback if you want to avoid that overhead.

I was using the Twisted deferred Interface quite extensively last year via Tornado and didn’t notice any particularly noticeable performance impact as a result. The Couchbase performance will be much more impacted by the overhead of the event loop itself and other stuff running on the loop.

Gevent and Twisted are very different approaches to the same problem so I’d actually recommend looking at them in isolation from the Couchbase SDK to see which one fits your use case better. AsyncBucket is just the base class that the gevent/twisted interfaces are implemented from.