Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 959
skalski Avatar asked Feb 15 '20 07:02

skalski


1 Answers

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.

like image 87
Kelvin Lawrence Avatar answered Oct 11 '22 22:10

Kelvin Lawrence