Why following Couchbase async Python code does not work?

亡梦爱人 提交于 2021-01-28 13:51:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!