Multi-threading


#1

Hi,

I have nginx configured to run 1 worker. uWSGI configured to use 2 threads and a flask app that queries a cb database. All this on a 2 core machine.

I configured one of the flask endpoints to run a loop just to make the request wait. While that request is running its get loop if I fire any other request - I get an ‘error not recognized’ message from couchbase.

Any help :smile:

Tnx


#2

error not recognized doesn’t seem like a Couchbase error. Can you please paste the applicable parts of the error?


#3

Apologies. Yes, your right - I built a logging and error handling wrapper and thats spitting out that error :smile:

From the documentation:

try:
    cb.get("foo")
except CouchbaseError as e:
    if e.is_data and isinstance(e, NotFoundError):
        # handle not found
        pass
    elif e.is_network:
        print("Got network error")
    elif e.is_data:
        print("Got other data-related error")
    else:
        print("Got unhandled error code")

The last part is where the error got caught.

I was able to solve it by using the lockmode=LOCKMODE_WAIT while accessing the singleton bucket.

I still have a multi-threading question for you though. Our API server, currently supports 4-5,000 users/connections simultaneously. When we push cb into prod, would using LOCKMODE_WAIT, still make sense? - As per the documentation, there doesn’t seem to be an alternative other than have a separate connection per thread.

Thanks!


#4

You can share connections per thread so long as two threads are not concurrently using the client library. The LOCKMODE_WAIT option will ensure that two threads do not concurrently use the same client.

Whether using one connection per thread, or using a connection pool (in which you have several connections, but not as many connections as threads) makes sense really depends on how frequently each of those threads will be using Couchbase. You can find an example of a connection pool with Couchbase in our examples directory in the github project: https://github.com/couchbase/couchbase-python-client/blob/master/examples/connection-pool.py

To be quite fair, if you are using that many Python threads, I would recommend you look at something like gevent which largely gives you the same programming model as Python threads, but is much more efficient under the hood, since it will actually use a high performance event loop underneath. You can use the gcouchbase module which works very well with gevent.

Python threads are known to be inefficient and will actually slow down your application.


#5

Thanks @mnunberg

We ended up sticking with LOCKMODE_WAIT for now. But will very soon be looking at gevent (well, maybe we wont need to).