问题
Consider following piece of code:
import asyncio
from acouchbase.cluster import Cluster
from couchbase.cluster import ClusterOptions
from couchbase.cluster import PasswordAuthenticator
async def do_crud_op():
cb = Cluster.connect("couchbase://localhost", options=ClusterOptions(PasswordAuthenticator("user", "password")))
cb = cb.bucket('customers')
await cb.upsert('id', {'some': 'value'})
return await cb.get('id')
loop = asyncio.get_event_loop()
rv = loop.run_until_complete(do_crud_op())
print(rv.value)
I am using Python 3.0 SDK and COuchbase 6.5.1 on Ubuntu 20.04. The above code gives me LCB_ERR_NO_CONFIGURATION
exception. Can someone help?
回答1:
Well, I found out that we need to wait on bucket variable using on_connect
method to make it work.
import asyncio
from acouchbase.cluster import Cluster
from couchbase.cluster import ClusterOptions
from couchbase.cluster import PasswordAuthenticator
async def do_crud_op():
cb = Cluster.connect("couchbase://localhost", options=ClusterOptions(PasswordAuthenticator("user", "password")))
cb = cb.bucket('customers')
await cb.on_connect()
await cb.upsert('id', {'some': 'value'})
return await cb.get('id')
loop = asyncio.get_event_loop()
rv = loop.run_until_complete(do_crud_op())
print(rv.value)
来源:https://stackoverflow.com/questions/62677463/why-following-couchbase-async-python-code-does-not-work