Details
-
Bug
-
Resolution: Fixed
-
Critical
-
4.0.0
-
None
-
None
-
1
-
SDK20: Pathfinding + Wrappers
Description
Running a query in a thread raises an exception:
from couchbase.cluster import Cluster |
from couchbase.options import ClusterOptions |
from couchbase.auth import PasswordAuthenticator |
import threading |
|
auth = PasswordAuthenticator("Administrator", "password") |
cluster = Cluster("couchbase://localhost", ClusterOptions(auth)) |
|
|
def fn(c): |
rows = c.query("SELECT * from `travel-sample` limit 10").rows() |
for r in rows: |
print(r) |
|
|
t = threading.Thread(target=fn, args=(cluster,)) |
t.start()
|
t.join()
|
results in:
Exception in thread Thread-1:
|
Traceback (most recent call last):
|
File "/Users/davidkelly/.pyenv/versions/3.9.5/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 954, in _bootstrap_inner
|
self.run()
|
File "/Users/davidkelly/.pyenv/versions/3.9.5/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 892, in run
|
self._target(*self._args, **self._kwargs)
|
File "/Users/davidkelly/projects/py_meetup/threads.py", line 12, in fn
|
res = c.query("SELECT * from `travel-sample` limit 10").rows()
|
File "/Users/davidkelly/.pyenv/versions/3.9.5/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/couchbase/cluster.py", line 387, in query
|
return QueryResult(N1QLRequest.generate_n1ql_request(self.connection,
|
File "/Users/davidkelly/.pyenv/versions/3.9.5/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/couchbase/n1ql.py", line 44, in generate_n1ql_request
|
return cls(connection, query_params, row_factory=row_factory, **kwargs)
|
File "/Users/davidkelly/.pyenv/versions/3.9.5/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/couchbase/n1ql.py", line 40, in __init__
|
super().__init__(connection, query_params, row_factory=row_factory, **kwargs)
|
File "/Users/davidkelly/.pyenv/versions/3.9.5/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/couchbase/logic/n1ql.py", line 707, in __init__
|
self._rows = asyncio.Queue()
|
File "/Users/davidkelly/.pyenv/versions/3.9.5/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/queues.py", line 35, in __init__
|
self._loop = events.get_event_loop()
|
File "/Users/davidkelly/.pyenv/versions/3.9.5/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/events.py", line 642, in get_event_loop
|
raise RuntimeError('There is no current event loop in thread %r.'
|
RuntimeError: There is no current event loop in thread 'Thread-1'.
|
I believe we are assuming we are in Asyncio/twisted context, or in the main thread, when creating the Asyncio Queue for streaming the results. Should be just a matter of correcting how we create the queue (call_soon_threadsafe? something else?)
Since FastApi uses threads, this is worth prioritizing as that is a pretty popular framework, and perhaps others use threading as well.
Attachments
Issue Links
- relates to
-
PYCBC-1413 Streaming API raise exception when executing in thread
- Resolved