简体   繁体   English

为什么以下 Couchbase 异步 Python 代码不起作用?

[英]Why following Couchbase async Python code does not work?

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.我在 Ubuntu 20.04 上使用 Python 3.0 SDK 和 Couchbase 6.5.1。 The above code gives me LCB_ERR_NO_CONFIGURATION exception.上面的代码给了我LCB_ERR_NO_CONFIGURATION异常。 Can someone help?有人可以帮忙吗?

Well, I found out that we need to wait on bucket variable using on_connect method to make it work.好吧,我发现我们需要使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM