Async couchbase call to atomic counter failing

I am using couchbase 3.0.10 (Python 3.9)

Have been using acouchbase for all operations (including single and multi inserts, upserts and removal).
But the counter function fails and throws an exception.

result = await collection.counter("doc_id", delta=1)

TypeError: object AsyncResult can’t be used in ‘await’ expression

Use the Asynchronous APIs for async functions Near the bottom of the navigation on the left Welcome to the Couchbase Python SDK documentation! — Couchbase Python Client Library 4.1.9 documentation The only differences is ‘acouchbase.cluster’

from acouchbase.cluster import Cluster

import asyncio
import time
import json
from couchbase.options import (ClusterOptions, ClusterTimeoutOptions, IncrementOptions, DeltaValue)
from acouchbase.cluster import Cluster
from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import QueryOptions

async def main():
    print(f"started at {time.strftime('%X')}")
    cluster = Cluster('couchbase://127.0.0.1', ClusterOptions(PasswordAuthenticator('Administrator', 'password')))
    cb = cluster.bucket('my_bucket')
    collection = cb.default_collection()
    result2 = await collection.binary().increment("doc_id", IncrementOptions(delta=DeltaValue(5)))
    print(f"finished at {time.strftime('%X')} {result2}")

asyncio.run(main())
started at 09:45:47
finished at 09:45:47 CounterResult:{'cas': 1685119547093090304, 'mutation_token': <pycbc_core.mutation_token object at 0x1065f1bd0>, 'content': 80}

Btw - you would likely benefit from using a newer version of the Python SDK.

This does not seem to work with couchbase 3.0.10

I tried something similar but it throws an exception

    import time
    from couchbase.collection import (IncrementOptions, DeltaValue)
    from acouchbase.cluster import Cluster
    from couchbase.cluster import ClusterOptions, PasswordAuthenticator
    
    print(f"started at {time.strftime('%X')}")
    
    cluster = Cluster('couchbase://127.0.0.1', options=ClusterOptions(PasswordAuthenticator('Administrator', 'password')))
    cb = cluster.bucket('my_bucket')
    collection = cb.default_collection()
    result2 = await collection.binary().increment("doc_id", IncrementOptions(delta=DeltaValue(5)))
    print(f"finished at {time.strftime('%X')} {result2}")

couchbase.exceptions.ClientTemporaryFailException: <RC=0x3F8[LCB_ERR_NO_CONFIGURATION (1016)], There was a problem scheduling your request, or determining the appropriate server or vBucket for the key(s) requested. This may also be a bug in the SDK if there are no network issues, C Source=(src/counter.c,118)>

The message indicates it cannot connect to the cb server. You will need to change the address, bucket name and username/password to yours.

I have put dummy credentials for the sake of the post. I had missed out on adding :
await cb.on_connect()
Here is the updated code:

    import time
    from couchbase.collection import (IncrementOptions, DeltaValue)
    from acouchbase.cluster import Cluster
    from couchbase.cluster import ClusterOptions, PasswordAuthenticator
    
    print(f"started at {time.strftime('%X')}")
    
    cluster = Cluster('couchbase://127.0.0.1', options=ClusterOptions(PasswordAuthenticator('Administrator', 'password')))
    cb = cluster.bucket('my_bucket')
    await cb.on_connect()
    collection = cb.default_collection()
    result2 = await collection.binary().increment("doc_id", IncrementOptions(delta=DeltaValue(5)))
    print(f"finished at {time.strftime('%X')} {result2}")

This is the exception:

result2 = await collection.binary().increment("doc_id", IncrementOptions(delta=DeltaValue(5)))
TypeError: object AsyncMutationResult can't be used in 'await' expression
Exception ignored in tp_clear of: <class 'TypeError'>

Use the Couchbase Python Client Library 4.1.4 with my example. It works.

if you still have issues open a ticket at issues.couchbase.com

Hi @dishawagle – We added support for binary operations to the acouchbase API in 3.2.4. The changes can be found in PYCBC-1210.

Thank You. I was able to upgrade the API to 4.1.4 and make it work.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.