Using gremlinclient

Before you get started, make sure you have the Gremlin Server up and running. All of the following examples use the Tornado client with PEP 492 Python 3.5 async/await syntax, but they can all be adjusted as shown in the Using aiohttp and Tornado Asyncio Integration sections.

Simple API

Submit a script with submit:

>>> async def do_submit():
...     resp = await submit(
...         "ws://localhost:8182/", "1 + 1")
...     while True:
...         msg = await resp.read()
...         if msg is None:
...             break  # connection closes automatically
...         print(msg)

Get a database connection with create_connection:

>>> async def get_conn():
...     conn = await create_connection("ws://localhost:8182/")
...     resp = conn.send(
...         "ws://localhost:8182/", "1 + 1")
...     while True:
...         msg = await resp.read()
...         if msg is None:
...             break
...     conn.close()  # make sure conn is closed when done

The GraphDatabase object

Get a database connection from GraphDatabase:

>>> async def get_conn():
...     graph = GraphDatabase("ws://localhost:8182/")
...     conn = await graph.connect()
...     ...
...     conn.close()

Get a database session connection from GraphDatabase:

>>> async def get_conn():
  ...     graph = GraphDatabase("ws://localhost:8182/")
  ...     sess = await graph.session()  # session inherits from Connection
  ...     ...
  ...     sess.close()

The Pool object

Reuse websocket connections with Pool:

>>> async def get_conn():
...     pool = Pool("ws://localhost:8182/")
...     conn = await pool.acquire()
...     ...
...     pool.release(conn)
...     pool.close()  # Close all released conns

Automatically release connections to Pool after read:

>>> async def get_conn():
...     pool = Pool("ws://localhost:8182/", force_release=True)
...     conn = await pool.acquire()
...     resp = conn.send("1 + 1")
...     while True:
...         msg = await resp.read()
...         if msg is None:
...             break  # conn is automatically released to pool.
...      pool.close()

For more info, see the Tornado Client docs

The RemoteConnection object

The remote connection object provides a synchronous interface designed to be used with the official TinkerPop Gremlin-Python Gremlin Language Variant (GLV):

>>> from gremlin_python import PythonGraphTraversalSource, GroovyTranslator  # imports may change after packaging
>>> from gremlinclient.tornado_client import RemoteConnection
>>> remote_conn = RemoteConnection("ws://localhost:8182/")
>>> translator = GroovyTranslator("g")
>>> g = PythonGraphTraversalSource(translator,
...                                remote_connection=remote_conn)

This allows you to write Gremlin traversals using pure Python!:

>>> g.addV('person').property('name','stephen').next()
>>> g.V().toList()

Remember to call next() or toList() to submit the traversal to the server.

For more info see aiohttp_client.RemoteConnection and tornado_client.RemoteConnection