【问题标题】:'NoneType' object is not callable - while starting Flask application'NoneType' 对象不可调用 - 在启动 Flask 应用程序时
【发布时间】:2019-11-29 13:22:48
【问题描述】:

访问烧瓶应用程序的 graphql 端点时出现以下错误。

http://0.0.0.0:5000/graphql

TypeError: 'NoneType' object is not callable
[20190721 13:00:12:974 _internal.py:88 ERROR] Error on request:
Traceback (most recent call last):
  File "/usr/local/python/python-2.7/std/lib/python2.7/site-packages/werkzeug/serving.py", line 270, in run_wsgi
    execute(self.server.app)
  File "/usr/local/python/python-2.7/std/lib/python2.7/site-packages/werkzeug/serving.py", line 258, in execute
    application_iter = app(environ, start_response)
  File "/usr/local/python/python-2.7/std/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/python/python-2.7/std/lib/python2.7/site-packages/flask/app.py", line 1989, in wsgi_app
    return response(environ, start_response)
TypeError: 'NoneType' object is not callable

应用代码:

import graphene
from   apis.graphql.schema.queries \
                                import Query
from   flask                    import Flask
from   flask_graphql            import GraphQLView

import time
from prometheus_client import Counter, Histogram, start_http_server


FLASK_REQUEST_LATENCY = Histogram('flask_request_latency_seconds', 'Flask Request Latency', ['method', 'endpoint'])
FLASK_REQUEST_COUNT = Counter('flask_request_count', 'Flask Request Count', ['method', 'endpoint', 'http_status'])


def get_options():
    """Function to get the options passed via command line."""
    from cmdline.parse import OptionParser
    parser = OptionParser()

    # Add the port option with default value 5000
    parser.add_option(
        '-port',
        required=False,
        type=int,
        default=5002,
        help='Port on which to listen for incoming requests. '
             'Defaults to 5000.'
    )

    opts, args = parser.parse_args()
    return opts


def before_request():
    request.start_time = time.time()


def after_request(response):
    request_latency = time.time() - request.start_time
    FLASK_REQUEST_LATENCY.labels(request.method, request.path).observe(request_latency)
    FLASK_REQUEST_COUNT.labels(request.method, request.path, response.status_code).inc()

def monitor(app, port, addr=''):
    app.before_request(before_request)
    app.after_request(after_request)
    start_http_server(port, addr)


if __name__ == '__main__':
    # Get the command line options.
    options = get_options()

    # Create an instance of the Flask class for the web app.
    app = Flask(__name__)

    # Adds the endpoint /graphql.
    app.add_url_rule(
        '/graphql',
        view_func=GraphQLView.as_view(
            'graphql', schema=graphene.Schema(Query), graphiql=True
        )
    )

    monitor(app, options.port)

    app.run(
        host = '0.0.0.0',
        port = 5000,
        threaded=True
        )

从上面的代码中,如果我删除应用程序的监视器装饰,那么烧瓶应用程序工作得非常好,并且 URL /graphql 会打开石墨烯 UI。但是一旦我添加了监控功能,它就会抛出上述错误。

【问题讨论】:

  • 它似乎在你得到response = None的地方,现在执行response(..)意味着None(..)。也许你在某些函数中忘记了return value,所以现在它返回默认值None

标签: python flask graphql prometheus


【解决方案1】:

您的after_request 函数需要返回响应。

def after_request(response):
    request_latency = time.time() - request.start_time
    FLASK_REQUEST_LATENCY.labels(request.method, request.path).observe(request_latency)
    FLASK_REQUEST_COUNT.labels(request.method, request.path, response.status_code).inc()
    return response

【讨论】:

    猜你喜欢
    • 2012-08-10
    • 2019-04-14
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多