有没有一种方法可以在异步Python应用程序中使用Gremlin?

时间:2020-02-15 07:30:27

标签: gremlin tinkerpop gremlinpython

TinkerPop文档描述了Python的GLV。但是,其中提供的示例围绕同步代码构建。有一个aiogremlin库旨在允许在Python的异步代码中使用Gremlin。不幸的是,该项目似乎已终止。

官方GLV是否支持asyncio或是否可以在异步Python应用程序中使用Gremlin?

1 个答案:

答案 0 :(得分:0)

我注意到这个问题没有得到解答,所以这里......

今天的 Gremlin Python 客户端使用 Tornado。将来可能会更改为仅使用 aiohttp。让事件循环很好地协同工作可能很棘手。我发现的最简单的方法是使用 nest-asyncio 库。安装后你可以写这样的东西。我没有显示正在创建的 g,但此代码假定已建立与服务器的连接,并且 g 是图遍历源。

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))     

正如你提到的,另一个选择是使用 aiogremlin 之类的东西。