[英]'NoneType' object is not callable - while starting Flask application
訪問燒瓶應用程序的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
)
從上面的代碼中,如果我刪除了應用程序的顯示器裝飾,那么flask應用程序可以很好地工作,URL / graphql打開石墨烯UI。 但是,一旦我添加了監視功能,它就會引發上述錯誤。
您的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.