简体   繁体   English

如何使用在 gunicorn 上运行的 pycharm 2.x 调试 flask.app

[英]How to debug flask.app with pycharm 2.x that's running on gunicorn

I'm developing a flask.app that uses web socket functionality and installed flask-socket to provide that.我正在开发一个flask.app,它使用网络套接字功能并安装了flask-socket来提供它。 So the flask-socket developer recommends gunicorn as web server.所以flask-socket 开发者推荐gunicorn作为web 服务器。 My question is now how to connect the remove debugger of pycharm with gunicorn to intercept the execution with breakpoints.我现在的问题是如何将pycharm的remove debugger与gunicorn连接起来,用断点拦截执行。

Settings > Project Settings > Python Debugger

There's an option in there to enable "gevent compatible debugging".那里有一个选项可以启用“gevent 兼容调试”。

Then, go into your debugger settings (shortcut is through the toolbar, click the dropdown near the play/debug icons and select "Edit Configurations"然后,进入调试器设置(快捷方式是通过工具栏,单击播放/调试图标附近的下拉菜单,然后选择“编辑配置”

Set the "Script" to your virtualenv's isntallation of gunicorn, something like:将“脚本”设置为您的 virtualenv 对 gunicorn 的安装,例如:

/Users/iandouglas/.virtualenvs/defaultenv/bin/gunicorn

Set the "Script Parameters" to something like -b 192.168.1.1:9000 app:yourappname (assuming your primary starting script is called app.py and you're refering to it as 'yourappname'将“脚本参数”设置为-b 192.168.1.1:9000 app:yourappname (假设您的主要启动脚本名为 app.py 并且您将其称为“yourappname”

the "Working directory" will be automatically set otherwise set it to wherever your code lives: /Users/iandouglas/PycharmProjects/MyExampleApp “工作目录”将自动设置,否则将其设置为代码所在的位置: /Users/iandouglas/PycharmProjects/MyExampleApp

I have a separate config file for my gunicorn settings, which specifies a host/port but I still had to specify the -b 0.0.0.0:5001 parameter to force gunicorn to bind to all IPs on my machine on port 5001.我的 gunicorn 设置有一个单独的配置文件,它指定了一个主机/端口,但我仍然必须指定-b 0.0.0.0:5001参数来强制 gunicorn 绑定到我机器上端口 5001 上的所有 IP。

ps ps

One important step is to add this envvar as pointed out here一个重要的步骤是添加这里指出的这个 envvar

PYDEVD_USE_CYTHON=NO

My case for PyCharm 2018.1.3 Professional:我的 PyCharm 2018.1.3 Professional 案例:

  1. Go to run/debug configurations creating-and-editing-run-debug-configurations转到运行/调试配置创建和编辑运行调试配置

  2. Choose new "Python" config选择新的“Python”配置

  3. Script path: your_path_to_/venv/bin/gunicorn脚本路径:your_path_to_/venv/bin/gunicorn
  4. Parameters(for my case): -b :5001 --access-logfile - --error-logfile - "run:create_application()"参数(对于我的情况):- -b :5001 --access-logfile - --error-logfile - "run:create_application()"
  5. Python interpreter: your venv python version for project Python 解释器:项目的 venv python 版本
  6. Working directory: path to your project工作目录:项目路径
  7. Save and press DEBUG (Shift+F9)保存并按 DEBUG (Shift+F9)
  8. be happy!快乐!

I was trying to debug on Pycharm 2020.2.1 and the break points weren't correctly working even though the Gevent compatible debugging was enabled.我试图在 Pycharm 2020.2.1 上进行调试,即使启用了 Gevent 兼容调试,断点也无法正常工作。 It turned out that I had to disable Cython for my run configuration via setting the environment variable as described here to make it work.事实证明,我必须按照此处所述设置环境变量来为我的运行配置禁用 Cython 以使其工作。

PYDEVD_USE_CYTHON=NO
  • edit your flask launch python file编辑您的烧瓶启动 python 文件
$ vim manage.py
  • remove debug option setting删除调试选项设置
from web import app
import sys

if __name__ == '__main__':
    # app.run(host='0.0.0.0', port=app.config["PORT"], debug=app.config["DEBUG"])
    app.run(host='0.0.0.0', port=9998, debug=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM