I'm developing a flask.app that uses web socket functionality and installed flask-socket to provide that. So the flask-socket developer recommends gunicorn as web server. My question is now how to connect the remove debugger of pycharm with gunicorn to intercept the execution with breakpoints.
问题:
回答1:
Settings > Project Settings > Python Debugger
There's an option in there to enable "gevent compatible debugging".
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:
/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'
the "Working directory" will be automatically set otherwise set it to wherever your code lives: /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.
回答2:
My case for PyCharm 2018.1.3 Professional:
Go to run/debug configurations creating-and-editing-run-debug-configurations
Choose new "Python" config
- Script path: your_path_to_/venv/bin/gunicorn
- Parameters(for my case):
-b :5001 --access-logfile - --error-logfile - "run:create_application()"
- Python interpreter: your venv python version for project
- Working directory: path to your project
- Save and press DEBUG (Shift+F9)
- be happy!
回答3:
- edit your flask launch python file
$ 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)