【发布时间】:2020-08-03 17:52:53
【问题描述】:
我有一个 Flask Web 应用程序,我想在其中保持与 AWS Neptune 图形数据库的持久连接。该连接建立如下:
from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
neptune_endpt = 'db-instance-x.xxxxxxxxxx.xx-xxxxx-x.neptune.amazonaws.com'
remoteConn = DriverRemoteConnection(f'wss://{neptune_endpt}:8182/gremlin','g')
self.g = traversal().withRemote(remoteConn)
我面临的问题是,如果闲置,连接会自动断开,并且我找不到检测连接是否断开的方法(以便我可以使用上面的代码 sn-p 重新连接)。
我看到了类似的问题:Gremlin server withRemote connection closed - how to reconnect automatically? 但是这个问题也没有解决方案。这个similar question也没有答案。
我尝试了以下两种解决方案(均无效):
- 我将我的 web 应用设置在 4 个 Gunicorn 工作器后面,超时时间为 100 秒,希望工作器重新启动能够处理 Gremlin 超时。
- 我尝试捕获异常以检测连接是否断开。每次我使用
self.g在我的图表上进行一些遍历时,我都会尝试“刷新”连接,我的意思是:
def _refresh_neptune(self):
try:
self.g = traversal().withRemote(self.conn)
except:
self.conn = DriverRemoteConnection(f'wss://{neptune_endpt}:8182/gremlin','g')
self.g = traversal().withRemote(self.conn)
这里self.conn被初始化为:
self.conn = DriverRemoteConnection(f'wss://{neptune_endpt}:8182/gremlin','g')
有什么办法可以解决这个连接错误?
谢谢
更新:添加以下错误信息:
File "/home/ubuntu/.virtualenvs/rundev/lib/python3.6/site-packages/gremlin_python/process/traversal.py
", line 58, in toList
return list(iter(self))
File "/home/ubuntu/.virtualenvs/rundev/lib/python3.6/site-packages/gremlin_python/process/traversal.py
", line 48, in __next__
self.traversal_strategies.apply_strategies(self)
File "/home/ubuntu/.virtualenvs/rundev/lib/python3.6/site-packages/gremlin_python/process/traversal.py
", line 573, in apply_strategies
traversal_strategy.apply(traversal)
File "/home/ubuntu/.virtualenvs/rundev/lib/python3.6/site-packages/gremlin_python/driver/remote_connec
tion.py", line 149, in apply
remote_traversal = self.remote_connection.submit(traversal.bytecode)
File "/home/ubuntu/.virtualenvs/rundev/lib/python3.6/site-packages/gremlin_python/driver/driver_remote
_connection.py", line 56, in submit
results = result_set.all().result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 425, in result
return self.__get_result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/home/ubuntu/.virtualenvs/rundev/lib/python3.6/site-packages/gremlin_python/driver/resultset.py"
, line 90, in cb
f.result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 425, in result
return self.__get_result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/ubuntu/.virtualenvs/rundev/lib/python3.6/site-packages/gremlin_python/driver/connection.py
", line 83, in _receive
status_code = self._protocol.data_received(data, self._results)
File "/home/ubuntu/.virtualenvs/rundev/lib/python3.6/site-packages/gremlin_python/driver/protocol.py",
line 81, in data_received
'message': 'Server disconnected - please try to reconnect', 'attributes': {}})
gremlin_python.driver.protocol.GremlinServerError: 500: Server disconnected - please try to reconnect
【问题讨论】:
-
需要多长时间才能看到连接错误?您的集群中是否启用了 IAM Auth?
-
遇到同样的错误。有什么解决办法吗?
-
@vissu 唯一对我有用的解决方法是每次我的 API 需要操作/查询 Neptune DB 时重新创建
remoteConn对象,然后在 @987654331 上调用close()@对象一旦完成。这增加了延迟,但这是唯一对我有用的东西。 (调用close()的原因是,否则您的代码将开始抛出Too many open files错误)。
标签: python gremlin gremlin-server amazon-neptune gremlinpython