Gremlin Python Cannot run the event loop

Question:

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

Asked By: Aureon

||

Answers:

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)

Answered By: Kelvin Lawrence