为什么遵循Couchbase异步Python代码不起作用? [英] Why following Couchbase async Python code does not work?

查看:55
本文介绍了为什么遵循Couchbase异步Python代码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

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 异常.有人可以帮忙吗?

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?

推荐答案

好吧,我发现我们需要使用 on_connect 方法等待bucket变量才能起作用.

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)

这篇关于为什么遵循Couchbase异步Python代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆