我可以在 Jupyter Notebook 中使用 Gremlin Python 客户端并避免事件循环错误吗?

Kel*_*nce 2 gremlin tinkerpop amazon-neptune gremlinpython graph-notebook

我知道图形笔记本项目允许使用魔法命令提交 Gremlin 查询。但是,有时我需要在常规 Jupyter 笔记本单元中使用 Python 进行编码并使用代码连接到服务器。如果使用 Gremlin Python 3.5.2 客户端,我尝试执行以下操作:

server = '<your server endpoint goes here>'
port = 8182

endpoint = f'wss://{server}:{port}/gremlin'

connection = DriverRemoteConnection(endpoint,'g')

g = traversal().withRemote(connection)
Run Code Online (Sandbox Code Playgroud)

由于 Jupyter 事件循环已在运行,因此会引发错误。

有没有解决的办法?

Kel*_*nce 8

在创建远程连接时可以指定一个附加参数,告诉 Python 客户端嵌套事件循环。您只需按照以下方式创建连接:


from gremlin_python.driver.aiohttp.transport import AiohttpTransport

# Other imports not shown

server = '<your server endpoint goes here>'
port = 8182

endpoint = f'wss://{server}:{port}/gremlin'
print(endpoint)

connection = DriverRemoteConnection(endpoint,'g',
                 transport_factory=lambda:AiohttpTransport(call_from_event_loop=True))

g = traversal().withRemote(connection)
Run Code Online (Sandbox Code Playgroud)

关键区别在于,transport_factory提供的自定义实际上只是lambda常规 的包装AiohttpTransportcall_from_event_loop参数设置为True

此额外配置告诉 Gremlin Python 客户端应用适当的内部更改来嵌套事件循环。