Having Issue in connecting to CB in my local windows enviroment

Having Issue in connecting to CB with python sdk 4.1.4 in my local windows enviroment

CB - Version 4.1.4
Python Version 3.8.10

installed with pip install couchbase==4.1.4

Sample code trying to test : - No issue in code (works on other env)

import asyncio
import time
import json
from acouchbase.cluster import Cluster
from acouchbase.bucket import AsyncBucket
from couchbase.auth import PasswordAuthenticator
from couchbase.options import ClusterOptions
CB_APPS_USER=“user”
CB_APPS_PASSWORD=“*****”
CB_URL=“couchbase://0.0.0.0”
CB_INSTANCE=“cb_instance”

async def main():
print(f"started at {time.strftime(‘%X’)}“)
auth = PasswordAuthenticator(CB_APPS_USER, CB_APPS_PASSWORD)
cluster = await Cluster.connect(CB_URL,ClusterOptions(auth))
bucket = AsyncBucket(cluster, CB_INSTANCE)
await bucket.on_connect()
dfc = bucket.default_collection()
#await bucket.get_multi([“testabc”], quiet=False)
print(f"finished at {time.strftime(‘%X’)} {6}”)

asyncio.run(main())

Error:
Traceback (most recent call last):
File “.\main.py”, line 23, in
asyncio.run(main())
File “d:\users\pachari\appdata\local\programs\python\python38\lib\asyncio\runners.py”, line 44, in run
return loop.run_until_complete(main)
File “d:\users\pachari\appdata\local\programs\python\python38\lib\asyncio\base_events.py”, line 616, in run_until_complete
return future.result()
File “.\main.py”, line 16, in main
cluster = await Cluster.connect(CB_URL,ClusterOptions(auth))
File “D:\FliptRx\testcb\venv\lib\site-packages\acouchbase\cluster.py”, line 313, in connect
cluster = AsyncCluster(connstr, *options, **kwargs)
File “D:\FliptRx\testcb\venv\lib\site-packages\acouchbase\cluster.py”, line 69, in init
self._loop = self.get_loop(kwargs.pop(“loop”, None))
File “D:\FliptRx\testcb\venv\lib\site-packages\acouchbase\cluster.py”, line 84, in get_loop
loop = get_event_loop()
File "D:\FliptRx\testcb\venv\lib\site-packages\acouchbase_init
.py", line 73, in get_event_loop
return LoopValidator.get_event_loop(evloop)
File "D:\FliptRx\testcb\venv\lib\site-packages\acouchbase_init
.py", line 52, in get_event_loop
return LoopValidator.get_working_loop()
File "D:\FliptRx\testcb\venv\lib\site-packages\acouchbase_init
.py", line 29, in _get_working_loop
evloop.close()
File “d:\users\pachari\appdata\local\programs\python\python38\lib\asyncio\proactor_events.py”, line 674, in close
raise RuntimeError(“Cannot close a running event loop”)
RuntimeError: Cannot close a running event loop

i have posted 0.0.0.0 just to hide the actual IP.
There is no issue in credentials… nor in the code… as the code works fine in others dev envirnments. Issue is i am getting this error in my local env.

I created fresh venv –
installed using – pip install couchbase==4.1.4
installation was successfull
when i run the code i get the above error.

Hi @achari.prashant – Can you follow the examples we provide in the Github repo (one from the README below)? We have not yet added a custom event loop policy and until we do, obtaining the event loop via our provided get_event_loop method is what will be needed on Windows.

from acouchbase.cluster import Cluster, get_event_loop
from couchbase.options import ClusterOptions
from couchbase.auth import PasswordAuthenticator


async def write_and_read(key, value):
    cluster = await Cluster.connect('couchbase://localhost',
                      ClusterOptions(PasswordAuthenticator('Administrator', 'password')))
    cb = cluster.bucket('default')
    await cb.on_connect()
    cb_coll = cb.default_collection()
    await cb_coll.upsert(key, value)
    result = await cb_coll.get(key)
    return result

loop = get_event_loop()
rv = loop.run_until_complete(write_and_read('foo', 'bar'))
print(rv.content_as[str])

References:

1 Like

@jcasey still dose not work… getting same error:

Traceback (most recent call last):
File “D:\FliptRx\cbtest1\main2.py”, line 34, in
rv = loop.run_until_complete(write_and_read(‘foo’, ‘bar’))
File “d:\users\pachari\appdata\local\programs\python\python39\lib\asyncio\base_events.py”, line 642, in run_until_complete
return future.result()
File “D:\FliptRx\cbtest1\main2.py”, line 24, in write_and_read
cluster = await Cluster.connect(CB_URL,
File “D:\FliptRx\cbtest1\venv\lib\site-packages\acouchbase\cluster.py”, line 773, in connect
cluster = AsyncCluster(connstr, *options, **kwargs)
File “D:\FliptRx\cbtest1\venv\lib\site-packages\acouchbase\cluster.py”, line 100, in init
self._loop = self.get_loop(kwargs.pop(“loop”, None))
File “D:\FliptRx\cbtest1\venv\lib\site-packages\acouchbase\cluster.py”, line 115, in get_loop
loop = get_event_loop()
File "D:\FliptRx\cbtest1\venv\lib\site-packages\acouchbase_init
.py", line 73, in get_event_loop
return LoopValidator.get_event_loop(evloop)
File "D:\FliptRx\cbtest1\venv\lib\site-packages\acouchbase_init
.py", line 52, in get_event_loop
return LoopValidator.get_working_loop()
File "D:\FliptRx\cbtest1\venv\lib\site-packages\acouchbase_init
.py", line 29, in _get_working_loop
evloop.close()
File “d:\users\pachari\appdata\local\programs\python\python39\lib\asyncio\proactor_events.py”, line 674, in close
raise RuntimeError(“Cannot close a running event loop”)
RuntimeError: Cannot close a running event loop

Hi @achari.prashant – Can you show the entire code snippet? I do not see any issues w/ the examples when executed on a Windows machine.

1 Like

Hey @jcasey its working… i was using asyncio.get_event_loop() before… your exact code works… thanks

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.