Gavin Aten created TINKERPOP-2755:
-------------------------------------
Summary: gremlinpython gives error "RuntimeError: Cannot run the
event loop while another loop is running".
Key: TINKERPOP-2755
URL: https://issues.apache.org/jira/browse/TINKERPOP-2755
Project: TinkerPop
Issue Type: Bug
Components: python
Affects Versions: 3.6.0
Environment: I am running the code inside Docker with Python 3.9 on
Ubuntu 20.04 with relevant package versions as follows:
gremlinpython==3.6.0
uvicorn==0.17.6
fastapi==0.78.0
Reporter: Gavin Aten
Fix For: 3.6.0
Im not sure if this is the correct place to post an issue. If not, I can
redirect this elsewhere.
I am trying to run a fastapi app with uvicorn but am struggling to run a
gremlinpython query inside. It seems like gremlinpython tries to start up a new
event loop while one is already running. Is it possible to get gremlinpython to
use the current event loop if there is one running?
The following code snippet works when running with python3 but fails when
running with uvicorn
```
from gremlin_python.structure.graph import Graph
from gremlin_python.driver.driver_remote_connection import
DriverRemoteConnection
from fastapi import FastAPI
def run_query(conn_string, vertex_id):
graph= Graph()
remoteConn= DriverRemoteConnection(conn_string, 'g')
g=graph.traversal().withRemote(remoteConn)
details=g.V(vertex_id).valueMap(True).toList()
print(details)
remoteConn.close()
return FastAPI()
conn_string = '<conn_string>'
vertex_id = <'vertex_id'>
app = run_query(conn_string, vertex_id)
# running with:
# uvicorn \{file_name}:app
# python3 \{file_name}
```
The error when running with uvicorn is as follows:
```
File "/code/./app/main2.py", line 20, in <module>
app = run_query(conn_string, vertex_id)
File "/code/./app/main2.py", line 11, in run_query
details = g.V(vertex_id).valueMap(True).toList()
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/process/traversal.py",
line 62, in toList
return self.to_list()
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/process/traversal.py",
line 65, in to_list
return list(iter(self))
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/process/traversal.py",
line 48, in __next__
self.traversal_strategies.apply_strategies(self)
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/process/traversal.py",
line 682, in apply_strategies
traversal_strategy.apply(traversal)
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/driver/remote_connection.py",
line 78, in apply
remote_traversal = self.remote_connection.submit(traversal.bytecode)
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/driver/driver_remote_connection.py",
line 102, in submit
result_set = self._client.submit(bytecode,
request_options=self._extract_request_options(bytecode))
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/driver/client.py", line
148, in submit
return self.submit_async(message, bindings=bindings,
request_options=request_options).result()
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/driver/client.py", line
180, in submit_async
return conn.write(message)
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/driver/connection.py",
line 55, in write
self.connect()
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/driver/connection.py",
line 45, in connect
self._transport.connect(self._url, self._headers)
File
"/usr/local/lib/python3.9/dist-packages/gremlin_python/driver/aiohttp/transport.py",
line 80, in connect
self._loop.run_until_complete(async_connect())
File "uvloop/loop.pyx", line 1495, in uvloop.loop.Loop.run_until_complete
File "uvloop/loop.pyx", line 1488, in uvloop.loop.Loop.run_until_complete
File "uvloop/loop.pyx", line 1377, in uvloop.loop.Loop.run_forever
File "uvloop/loop.pyx", line 515, in uvloop.loop.Loop._run
RuntimeError: Cannot run the event loop while another loop is running
```
--
This message was sent by Atlassian Jira
(v8.20.7#820007)