Python “需要帮助调试”;“内部服务器错误”;关于heroku应用程序由“引起”;“JSON可序列化”;
所以我正在为黑客新闻编写一个RESTful API。直到昨天,一切都很顺利。当我部署一些更改时,一切都很好。但突然间,它疯狂地抛出了500个错误Python “需要帮助调试”;“内部服务器错误”;关于heroku应用程序由“引起”;“JSON可序列化”;,python,json,serialization,heroku,flask,Python,Json,Serialization,Heroku,Flask,所以我正在为黑客新闻编写一个RESTful API。直到昨天,一切都很顺利。当我部署一些更改时,一切都很好。但突然间,它疯狂地抛出了500个错误 http://hnify.herokuapp.com/get/top - throws 500 http://hnify.herokuapp.com/get/newest - throws 500 http://hnify.herokuapp.com/get/best - throws 500 但是,http://hnify.herokuapp.co
http://hnify.herokuapp.com/get/top - throws 500
http://hnify.herokuapp.com/get/newest - throws 500
http://hnify.herokuapp.com/get/best - throws 500
但是,http://hnify.herokuapp.com/get/trends
不会抛出任何错误,并且工作正常
附录:在写这个问题时,trends
URL也开始抛出500错误
下面是heroku日志的部分结果:
2013-12-22T17:40:54.528521+00:00 heroku[web.1]: State changed from up to starting
2013-12-22T17:40:57.323574+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-12-22T17:40:57.826384+00:00 heroku[web.1]: Starting process with command `gunicorn app:app`
2013-12-22T17:40:57.952472+00:00 app[web.1]: 2013-12-22 17:40:57 [2] [INFO] Handling signal: term
2013-12-22T17:40:57.951784+00:00 app[web.1]: 2013-12-22 17:40:57 [7] [INFO] Worker exiting (pid: 7)
2013-12-22T17:40:57.958557+00:00 app[web.1]: 2013-12-22 17:40:57 [2] [INFO] Shutting down: Master
2013-12-22T17:40:58.831301+00:00 app[web.1]: 2013-12-22 17:40:58 [2] [INFO] Using worker: sync
2013-12-22T17:40:58.830659+00:00 app[web.1]: 2013-12-22 17:40:58 [2] [INFO] Starting gunicorn 18.0
2013-12-22T17:40:58.831219+00:00 app[web.1]: 2013-12-22 17:40:58 [2] [INFO] Listening at: http://0.0.0.0:12219 (2)
2013-12-22T17:40:58.838520+00:00 app[web.1]: 2013-12-22 17:40:58 [7] [INFO] Booting worker with pid: 7
2013-12-22T17:40:59.033671+00:00 heroku[web.1]: State changed from starting to up
2013-12-22T17:40:59.232862+00:00 heroku[web.1]: Process exited with status 0
2013-12-22T17:41:32.146515+00:00 heroku[router]: at=info method=GET path=/get/top host=hnify.herokuapp.com fwd="71.231.57.36" dyno=web.1 connect=1ms service=344ms status=503 bytes=72
2013-12-22T17:53:44.759150+00:00 heroku[router]: at=info method=GET path=/get/newest host=hnify.herokuapp.com fwd="71.231.57.36" dyno=web.1 connect=1ms service=328ms status=503 bytes=72
2013-12-22T17:54:07.674549+00:00 heroku[router]: at=info method=GET path=/get/trends host=hnify.herokuapp.com fwd="71.231.57.36" dyno=web.1 connect=7ms service=7056ms status=200 bytes=2121
2013-12-22T18:18:36.120842+00:00 heroku[router]: at=info method=GET path=/get/trends host=hnify.herokuapp.com fwd="71.231.57.36" dyno=web.1 connect=81ms service=35ms status=503 bytes=72
2013-12-22T18:18:43.977936+00:00 heroku[router]: at=info method=GET path=/main.css host=hnify.herokuapp.com fwd="71.231.57.36" dyno=web.1 connect=1ms service=4ms status=404 bytes=72
2013-12-22T18:18:43.661406+00:00 heroku[router]: at=info method=GET path=/ host=hnify.herokuapp.com fwd="71.231.57.36" dyno=web.1 connect=2ms service=17ms status=200 bytes=2927
2013-12-22T18:18:47.605158+00:00 heroku[router]: at=info method=GET path=/get/top host=hnify.herokuapp.com fwd="71.231.57.36" dyno=web.1 connect=1ms service=3ms status=503 bytes=72
2013-12-22T18:18:40.786325+00:00 heroku[router]: at=info method=GET path=/get/ host=hnify.herokuapp.com fwd="71.231.57.36" dyno=web.1 connect=3ms service=4ms status=404 bytes=72
2013-12-22T18:26:52.188789+00:00 heroku[router]: at=info method=HEAD path=/get/best host=hnify.herokuapp.com fwd="216.46.175.33, 10.179.81.114, 54.241.198.78" dyno=web.1 connect=2ms service=2706ms status=503 bytes=0
2013-12-22T18:26:52.189318+00:00 heroku[router]: at=info method=HEAD path=/get/newest host=hnify.herokuapp.com fwd="216.46.175.37, 10.179.81.114, 184.169.203.101" dyno=web.1 connect=1ms service=2707ms status=503 bytes=0
2013-12-22T18:26:52.190210+00:00 heroku[router]: at=info method=HEAD path=/get/trends host=hnify.herokuapp.com fwd="216.46.190.190, 10.196.6.45, 50.18.102.132" dyno=web.1 connect=1ms service=633ms status=503 bytes=0
2013-12-22T18:26:53.569138+00:00 heroku[router]: at=info method=HEAD path=/get/best host=hnify.herokuapp.com fwd="216.46.175.36, 10.196.6.45, 54.241.198.78" dyno=web.1 connect=2ms service=1981ms status=503 bytes=0
2013-12-22T18:26:49.478860+00:00 heroku[router]: at=info method=HEAD path=/get/top host=hnify.herokuapp.com fwd="216.46.175.34, 10.179.81.114, 184.169.203.101" dyno=web.1 connect=2ms service=4ms status=503 bytes=0
2013-12-22T18:27:35.846872+00:00 heroku[router]: at=info method=HEAD path=/get/top host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=1ms service=3ms status=503 bytes=0
2013-12-22T18:27:36.343779+00:00 heroku[router]: at=info method=GET path=/get/top host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=1ms service=4ms status=503 bytes=72
2013-12-22T18:27:36.919042+00:00 heroku[router]: at=info method=HEAD path=/get/newest host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=10ms service=21ms status=503 bytes=0
2013-12-22T18:27:42.167121+00:00 heroku[router]: at=info method=GET path=/get/trends host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=1ms service=5ms status=503 bytes=72
2013-12-22T18:27:50.385923+00:00 heroku[router]: at=info method=HEAD path=/get/top host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=3ms service=7ms status=503 bytes=0
2013-12-22T18:27:50.955337+00:00 heroku[router]: at=info method=HEAD path=/get/newest host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=11ms service=9ms status=503 bytes=0
2013-12-22T18:27:53.414690+00:00 heroku[router]: at=info method=HEAD path=/get/trends host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=18ms service=27ms status=503 bytes=0
2013-12-22T18:27:52.794785+00:00 heroku[router]: at=info method=HEAD path=/get/best host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=3ms service=1276ms status=503 bytes=0
2013-12-22T18:28:31.135251+00:00 heroku[router]: at=info method=GET path=/get/top host=hnify.herokuapp.com fwd="46.236.24.52" dyno=web.1 connect=1ms service=3ms status=503 bytes=72
2013-12-22T18:28:31.469293+00:00 heroku[router]: at=info method=GET path=/get/newest host=hnify.herokuapp.com fwd="46.236.24.52" dyno=web.1 connect=1ms service=4ms status=503 bytes=72
2013-12-22T18:28:32.898799+00:00 heroku[router]: at=info method=GET path=/get/best host=hnify.herokuapp.com fwd="46.236.24.52" dyno=web.1 connect=1ms service=1152ms status=503 bytes=72
2013-12-22T18:26:52.191781+00:00 heroku[router]: at=info method=HEAD path=/get/newest host=hnify.herokuapp.com fwd="216.46.175.36, 10.196.6.45, 184.169.203.101" dyno=web.1 connect=4ms service=714ms status=503 bytes=0
2013-12-22T18:27:37.443588+00:00 heroku[router]: at=info method=GET path=/get/newest host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=1ms service=3ms status=503 bytes=72
2013-12-22T18:27:41.142385+00:00 heroku[router]: at=info method=GET path=/get/best host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=8ms service=1294ms status=503 bytes=72
2013-12-22T18:27:39.233665+00:00 heroku[router]: at=info method=HEAD path=/get/best host=hnify.herokuapp.com fwd="182.50.130.85" dyno=web.1 connect=2ms service=1262ms status=503 bytes=0
2013-12-22T18:29:56.874183+00:00 heroku[router]: at=info method=HEAD path=/get/top host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=1ms service=5ms status=503 bytes=0
2013-12-22T18:29:57.292349+00:00 heroku[router]: at=info method=HEAD path=/get/newest host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=3ms service=5ms status=503 bytes=0
2013-12-22T18:29:57.478167+00:00 heroku[router]: at=info method=GET path=/get/newest host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=1ms service=4ms status=503 bytes=72
2013-12-22T18:29:58.972529+00:00 heroku[router]: at=info method=HEAD path=/get/best host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=1ms service=1302ms status=503 bytes=0
2013-12-22T18:30:00.688709+00:00 heroku[router]: at=info method=HEAD path=/get/trends host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=2ms service=4ms status=503 bytes=0
2013-12-22T18:30:00.885436+00:00 heroku[router]: at=info method=GET path=/get/trends host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=1ms service=4ms status=503 bytes=72
2013-12-22T18:30:21.017547+00:00 heroku[router]: at=info method=HEAD path=/get/newest host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=1ms service=39ms status=503 bytes=0
2013-12-22T18:30:20.760432+00:00 heroku[router]: at=info method=HEAD path=/get/top host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=3ms service=6ms status=503 bytes=0
2013-12-22T18:30:22.593351+00:00 heroku[router]: at=info method=HEAD path=/get/trends host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=2ms service=5ms status=503 bytes=0
2013-12-22T18:29:57.084468+00:00 heroku[router]: at=info method=GET path=/get/top host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=1ms service=4ms status=503 bytes=72
2013-12-22T18:30:22.401968+00:00 heroku[router]: at=info method=HEAD path=/get/best host=hnify.herokuapp.com fwd="185.2.29.4" dyno=web.1 connect=5ms service=1177ms status=503 bytes=0
在我的记忆中,我甚至没有触摸现在抛出错误的调用
经过大量的打印和返回,我发现问题的核心是:
return jsonify(temp_cache['top']['response_json'])
以下是回溯:
Traceback (most recent call last):
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/HNify/app.py", line 67, in get_top
return jsonify(temp_cache['top']['response_json'])
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/json.py", line 238, in jsonify
indent=indent),
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/json.py", line 126, in dumps
rv = _json.dumps(obj, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 209, in encode
chunks = list(chunks)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 434, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
for chunk in chunks:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 332, in _iterencode_list
for chunk in chunks:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 442, in _iterencode
o = _default(o)
File "/Users/karan/Desktop/Dropbox/Codebase/General/envs/hnify/lib/python2.7/site-packages/flask/json.py", line 83, in default
return _json.JSONEncoder.default(self, o)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <Story: ID=6950891> is not JSON serializable
我尝试返回一个jsonify
ed列表,而不是dict
,但这会产生相同的错误。应用程序的完整代码为
有人能指出哪里出了问题吗
在研究这个问题时,我发现错误可能是由于将非ICT类型传递给jsonify造成的
这并不完全准确。不仅顶层对象必须是dict,而且任何引用的对象也必须是dict。堆栈跟踪表明顶级dict引用了类Story
的对象
它看起来像是在get_top()
中将其分配给缓存:
temp_cache['top']['response_json'] = {'stories': hn.get_stories()}
您需要添加一个函数,将故事
对象转换为字典,然后问题就会消失
此外,从您的描述来看,查找错误位置似乎花费了大量成本。考虑在HeloCU上添加一个在生产模式中激活的电子邮件记录器。这样,每次出现错误时,您都可以通过电子邮件获得堆栈跟踪
祝你好运。我提供自己的东西,而不是使用像Heroku这样的东西,所以这可能不适用,但这些奇怪的问题几乎总是因为我需要重新启动web服务器。另外,现在是在应用程序的部署脚本中添加功能测试和单元测试的好时机,以避免出现这种情况。@GarryCairns在发布问题之前,我确实重新启动了服务器。我想我应该把它贴出来。至于单元测试,我正在使用我编写的一个库来进行单元测试。无论如何,我也会尝试在这里写测试。我爱你,以一种非同性恋的方式!
temp_cache['top']['response_json'] = {'stories': hn.get_stories()}