为什么下面的 Couchbase async Python 代码不起作用?

Why following Couchbase async Python code does not 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.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)

我在 Ubuntu 20.04 上使用 Python 3.0 SDK 和 COuchbase 6.5.1。上面的代码给了我 LCB_ERR_NO_CONFIGURATION 异常。有人可以帮忙吗?

好吧,我发现我们需要使用 on_connect 方法等待存储桶变量使其工作。

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)