2

I am running Gremlin Python. Firstly, I did the installation in my local machine following these instructions here then I ran the code of this website here, but I got the following error at this bit:

    heading('SubgraphStrategy - just Texas airports')
    strategy = SubgraphStrategy(vertices=__.has("region","US-TX"), edges=__.hasLabel('route'))
    g2 = g.withStrategies(strategy)
    verts = g2.V().count().next()

RuntimeError: Cannot run the event loop while another loop is running

I verified my connection to a graph database to Gremlin Server, with the following code

%%graph_notebook_config
{
  "host": "localhost",
  "port": 8182,
  "ssl": false,
  "gremlin": {
    "traversal_source": "g"
  }
}

I found some solutions to the "RuntimeError: Cannot run the event loop while another loop is running", like the nest_async, but then I got a different error.

Thank you

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
Aureon
  • 141
  • 9

1 Answers1

3

If you are using Gremlin Python, inside a Jupyter notebook then an event loop will already be running. The 3.5.x and 3.6.x Gremlin Python clients have an option you can specify when you create your client object that will enable this to work. All you should need to do is something like this:

from gremlin_python.driver.aiohttp.transport import AiohttpTransport

# other common imports not shown

connection = DriverRemoteConnection(endpoint,'g',
                 transport_factory=lambda:AiohttpTransport(call_from_event_loop=True))

g = traversal().withRemote(connection)

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • Hi Kelvin, thank you I did the correction, but I got this error: `NameError: name 'AiohttpTransport' is not defined` after this query `verts = g2.V().count().next()` – Aureon Jul 30 '22 at 01:49
  • Which version of the Gremlin Python are you using? The example I shared needs at least the 3.5.0 version. – Kelvin Lawrence Jul 30 '22 at 01:54
  • Hi Kelvin, `gremlinpython==3.6.0`, the other libraries are: `graph-notebook==3.5.3` `graphql-core==3.2.1`. If you agree I can mark this answer as correct and I will make another post with the entire code as probably I am missing something. – Aureon Jul 30 '22 at 12:44
  • Yes, please. The answer as written should solve the event loop error. – Kelvin Lawrence Jul 30 '22 at 23:38
  • You also need to add an import though for `from gremlin_python.driver.aiohttp.transport import AiohttpTransport` - I will add that to the answer – Kelvin Lawrence Jul 30 '22 at 23:40
  • @KelvinLawrence I'm using fastAPI with uvicorn. but I got `ValueError: Can't patch loop of type ` – Alirezak Feb 01 '23 at 21:02
  • I am not familiar with any possible issues that the event loop patching library may have with the uvicorn web server's own event loop unfortunately. The Gremlin Python client uses the open source nest_asyncio package. Perhaps start there to see if you can spot any incompatibilities. The main goal for adding this option was to allow the client to run inside Jupyter Notebooks. – Kelvin Lawrence Feb 02 '23 at 22:46