Search code examples
dockergremlintinkerpopgremlin-servergremlinpython

Gremlin docker server connection not working


I'm running a gremlin server using the official docker container:

docker run --rm -it -p 8182:8182 --name gremlin tinkerpop/gremlin-server

I then try to run the following script from the host machine:

from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection


if __name__ == "__main__":

    g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182', 'g'))
    g.V().drop()
    g.V().addV('person')
    l = g.V().hasLabel('person')
    print(l.toList())

The connection seems to work (no errors), but the queries don't seem to be actually executed (the gremlin server statistics show no calls whatsoever).

The even more bizarre part is that the toList() call blocks execution, and returns nothing. If then I stop the docker container, the connection on the python side drops.

I'm using the default settings for the gremlin server. Could someone help me understand what's going on?

EDIT: I also tried changing the gremlin configuration host to 0.0.0.0.

EDIT: so the reason why it would appear that only the toList waits for an answer is because the other queries aren't actually executed yet, you need .next().


Solution

  • It turns out there were two errors:

    1. the address must end with /gremlin, so in my case 'ws://localhost:8182/gremlin'
    2. when trying this, an exception appears which looks like a connection error at first :
    RuntimeError: Event loop is closed
    Exception ignored in: <function ClientResponse.__del__ at 0x7fb532031af0>
    Traceback (most recent call last):
    [..]
      File "/usr/lib/python3.8/asyncio/selector_events.py", line 692, in close
      File "/usr/lib/python3.8/asyncio/base_events.py", line 719, in call_soon
      File "/usr/lib/python3.8/asyncio/base_events.py", line 508, in _check_closed
    RuntimeError: Event loop is closed
    

    this is actually not a connection error, but a warning that the connection was not properly closed. If you investigate, you would notice that the queries were in fact executed. The correct way to handle this is to write something along the lines of:

    conn = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g')
    g = traversal().withRemote(conn)
    [do your graph operations]
    conn.close()
    

    and with this, no exceptions, life is good. I am quite surprised this appears in no documentation.