Is there a way of using Gremlin within an asyncio Python application?
Asked Answered
O

2

8

The TinkerPop documentation describes GLV for Python. However, the examples presented in there are built around synchronous code. There is the aiogremlin library that was desingned to enable use of Gremlin in Python's asyncio code. Unfortunately, the project seem to be discontinued.

Does the official GLV support the asyncio or is there a way to use Gremlin in asynchronous Python applications?

Omegaomelet answered 15/2, 2020 at 7:30 Comment(0)
C
1

I noticed that this question has sat unanswered so here goes...

The Gremlin Python client today uses Tornado. That may change in the future to just use aiohttp. Getting the event loops to play nicely together can be tricky. The easiest way I have found is to use the nest-asyncio library. With that installed you can write something like this. I don't show the g being created but this code assumes the connection to the server has been made and that g is the Graph Traversal Source.

import nest_asyncio
nest_asyncio.apply() 

async def count_airports():
    c = g.V().hasLabel('airport').count().next()  
    print(c)
    
async def run_tests(g):
    await count_airports() 
    return
 
asyncio.run(run_tests(g))     

As you mentioned the other option is to use something like aiogremlin.

UPDATED 2023-08-29 : The latest versions of the Gremlin Python client now use AIOHTTP as their transport mechanism. This has very little impact on application programs, but it is an important change to be aware of. The client now has a parameter that can be used upon creation telling it to nest the event loops.

Calesta answered 13/4, 2021 at 14:55 Comment(0)
Y
0

Any latest version of the gremlin library supports async code but implementation doesn't seem to be straight, as for making the queries gremlinAsync uses future (Future Documentation)

To Make it easy to read and implement you can convert the Future object return by gremlin API to coroutine which support's await syntax

async def get_result(query, client):
    result = await asyncio.wrap_future(client.submitAsync(query))
    return result 

client = gremlin_connection(environ.get("url"),environ.get("username"),environ.get("password"))
data = await get_result(query1, client)
Yeager answered 23/2, 2022 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.