Tornado 在 chalice python 中抛出流关闭错误

Posted

技术标签:

【中文标题】Tornado 在 chalice python 中抛出流关闭错误【英文标题】:Tornado is throwing stream closed error in chalice python 【发布时间】:2020-03-03 05:35:33 【问题描述】:

我是第一次使用 Chalice 试用 AWS Neptune。

这是整个错误

Traceback (most recent call last):
  File "/var/task/chalice/app.py", line 1104, in _get_view_function_response
    response = view_function(**function_args)
  File "/var/task/app.py", line 44, in getPosts
    raise e
  File "/var/task/app.py", line 37, in getPosts
    result = g.V().has('name', 'test1').toList()
  File "/var/task/gremlin_python/process/traversal.py", line 58, in toList
    return list(iter(self))
  File "/var/task/gremlin_python/process/traversal.py", line 48, in __next__
    self.traversal_strategies.apply_strategies(self)
  File "/var/task/gremlin_python/process/traversal.py", line 573, in apply_strategies
    traversal_strategy.apply(traversal)
  File "/var/task/gremlin_python/driver/remote_connection.py", line 149, in apply
    remote_traversal = self.remote_connection.submit(traversal.bytecode)
  File "/var/task/gremlin_python/driver/driver_remote_connection.py", line 55, in submit
    result_set = self._client.submit(bytecode)
  File "/var/task/gremlin_python/driver/client.py", line 111, in submit
    return self.submitAsync(message, bindings=bindings).result()
  File "/var/task/gremlin_python/driver/client.py", line 127, in submitAsync
    return conn.write(message)
  File "/var/task/gremlin_python/driver/connection.py", line 55, in write
    self.connect()
  File "/var/task/gremlin_python/driver/connection.py", line 45, in connect
    self._transport.connect(self._url, self._headers)
  File "/var/task/gremlin_python/driver/tornado/transport.py", line 36, in connect
    lambda: websocket.websocket_connect(url))
  File "/var/task/tornado/ioloop.py", line 576, in run_sync
    return future_cell[0].result()
tornado.simple_httpclient.HTTPStreamClosedError: Stream closed

这是我的代码

import logging

from chalice import Chalice, BadRequestError, NotFoundError
from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import T, P, Operator
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from datetime import datetime

app = Chalice(app_name='chalice-neptune')
app.debug = True

logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)

def setup_graph():
    try:
        graph = Graph()
        connstring = 'ws://NEPTUNE-ENDPOINT-HERE:8182/gremlin'
        g = graph.traversal().withRemote(DriverRemoteConnection(connstring, 'g'))
        logging.info('Connected to Neptune')
    except Exception as e:
        logging.error(e, exc_info = True)
        raise BadRequestError("Could not connect to Neptune")
    return g

@app.route('/getPosts')
def getPosts():
    g = setup_graph()
    try:
        result = g.V().has('name', 'test1').toList()

        response = 
            'status_code': 200,
            'data': result
        
    except Exception as e:
        raise e
    return response

有人试过吗?

我已经按照这个桶里的例子gremlin-python-example

我知道我没有错过示例中的任何内容,但它仍然抛出流关闭错误。

【问题讨论】:

恐怕没用过Chalice(我已经成功用过Flink了)。一项观察是,您永远不会关闭 WS 连接,因此即使它正在工作,随着时间的推移,每次调用 getPosts 时,您都会泄漏 Web 套接字连接。您是否尝试过在setup_graph 内发送查询?另请注意,graph = Graph 方法现在已被弃用,取而代之的是使用traversal()。抱歉,您使用的是哪个版本的 Gremlin Python 客户端? @KelvinLawrence 我不知道traversal()。海王星文档仍然使用graph = Graph()。我正在使用gremlinpython 3.4.6 【参考方案1】:

显然我唯一改变的是我的连接字符串,它现在工作正常。

connstring = 'wss://NEPTUNE-ENDPOINT-HERE:8182/gremlin'

我把它从 ws 改成了 wss。

至于两者的区别可以参考这个答案

Difference between ws and wss?

【讨论】:

以上是关于Tornado 在 chalice python 中抛出流关闭错误的主要内容,如果未能解决你的问题,请参考以下文章

我无法在Tornado / Python上设置cookie

Python Tornado之四(Http层)

Python tornado初探

[Python][Tornado] 是不是可以在一个 python 程序中拥有多个不同端口和消息的 websocket?

Python Tornado框架(TCP层)

python之路45tornado的用法