调试线程Flask应用程序

时间:2018-06-07 10:47:08

标签: python debugging flask plotly-dash

我想使用ipdb(或其他python调试器)来调试使用Flask和Dash的应用程序。当我想使用函数将数据传递给Dash时,会出现问题(as mentioned here是一种在页面重新加载时提供更新的方法)。在多线程方面,Ipdb似乎有一些问题。有代码:

#!/usr/bin/env python

import json
import requests

from flask import Flask
from flask.globals import g

import dash
import dash_core_components as dcc
import dash_html_components as html

app = Flask(__name__)

dash_app = dash.Dash(server=app, url_base_pathname='/analytics/')


def get_analytics_data():
    return json.loads(
        requests.get('http://localhost:8000/api/').text
    )


def graphs():
    return (
        dcc.Graph(
            id='orders',
            figure={
                'data': [
                    {
                        'x': [key for key in get_analytics_data().keys()],
                        'y': [value for _, value in get_analytics_data().items()],
                        'name': 'restaurants',
                        'type': 'bar'
                    }
                ]
            }
        ),
    )


def serve_layout():
    import ipdb; ipdb.set_trace()
    return html.Div(children=[
        html.H1(children='Hello, dash'),
        *graphs()
    ])


dash_app.layout = serve_layout


if __name__ == '__main__':
    app.run(debug=True)

注意:我希望保留此app = Flask(__name__)以便进一步开发。

有堆栈跟踪: 。当我在页面上重新加载时会发生这种情况:

 * Debugger is active!
 * Debugger PIN: 224-366-292
127.0.0.1 - - [07/Jun/2018 12:32:53] "GET /analytics/ HTTP/1.1" 200 -
127.0.0.1 - - [07/Jun/2018 12:32:54] "GET /analytics/_dash-layout HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/dash/dash.py", line 160, in serve_layout
    layout = self._layout_value()
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/dash/dash.py", line 131, in _layout_value
    self._cached_layout = self._layout()
  File "/home/gonczor/Projects/analytics/analytics/main.py", line 53, in serve_layout
    return html.Div(children=[
  File "/home/gonczor/Projects/analytics/analytics/main.py", line 53, in serve_layout
    return html.Div(children=[
  File "/usr/lib/python3.6/bdb.py", line 48, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib/python3.6/bdb.py", line 66, in dispatch_line
    self.user_line(frame)
  File "/usr/lib/python3.6/pdb.py", line 261, in user_line
    self.interaction(frame, None)
  File "/home/gonczor/Projects/analytics/venv-analytics/lib/python3.6/site-packages/IPython/core/debugger.py", line 297, in interaction
    OldPdb.interaction(self, frame, traceback)
  File "/usr/lib/python3.6/pdb.py", line 344, in interaction
    signal.signal(signal.SIGINT, Pdb._previous_sigint_handler)
  File "/usr/lib/python3.6/signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread

我可以使用PyCharm的调试器成功调试应用程序,但它并不总是可用,所以我想要一些通用的控制台运行调试器。

修改

我已经完成了服务器创建过程,发现Falsk默认使用单线程服务器(并且单处理也是如此)。这在我的虚拟环境中run_simple()的{​​{1}}中进行处理。

0 个答案:

没有答案