Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one use the VSCode debugger to debug a Gunicorn worker process?

I have a GUnicorn/Falcon web service written in Python 3.4 on Ubuntu 14.04. I'd like to use the VSCode debugger to debug this service. I currently start the process with the command

/usr/local/bin/gunicorn --config /webapps/connects/routerservice_config.py routerservice:api

which starts routerservice.py using the config file routerservice_config.py. I have workers set to 1 in the config to keep it simple.

I've installed the Python extension to VSCode so I have the Python debugging tools. So how do I attach to the GUnicorn worker process or have VSCode run the startup command and auto attach.

Thanks, Greg

like image 202
Greg Enslow Avatar asked Oct 04 '16 00:10

Greg Enslow


People also ask

How do you debug a Gunicorn?

To debug a Python Flask app running in Gunicorn, we can set the DEBUG setting to True . to read the config from config.py with from_pyfile . to set the DEBUG setting to True to enable debugging in our Flask app.

How do you run in debug mode in Visual Studio Code?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

How do I debug a code in Visual Studio Python?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.


3 Answers

This launch.json setting worked for me on VScode 1.43:

{
    "name": "Python: Webapp",
    "type": "python",
    "request": "launch",
    "program": "/home/me/.virtualenvs/my-venv/bin/gunicorn",
    "gevent": true,
    "args": ["main:app", "--bind=127.0.0.1:8080", "--reload", "--worker-class", "eventlet", "-w", "1", "--timeout=7200"],
    "postDebugTask": "killdebugger"
}

Using this setting, I had to create a task for killing the python process after I stopped the debugger. This was simply because pressing the stop button would only close the debugger itself, while the Python process would keep on running. If you face the same, create a task by pressing F1, search for taskand click "Configure Task". Then add the following command to your tasks.json:

{
    "label": "killdebugger",
    "type": "shell",
    "command": "lsof -t -i tcp:8080 | xargs kill -9"
}

If you don't have this issue, remove the"postDebugTask": "killdebugger" setting from launch.json

like image 88
Mauricio Avatar answered Nov 13 '22 08:11

Mauricio


I'm the author of the extension. You could try the following: https://github.com/DonJayamanne/pythonVSCode/wiki/Debugging:-Remote-Debuging

  • Add the following code into your routerservice_config.py (or similar python startup file) import ptvsd ptvsd.enable_attach("my_secret", address = ('0.0.0.0', 3000))
  • Start the above application
  • Go into VS Code and then attach the debugger

FYI:
- This requires you to include the ptvsd package and configure it in your application.
- The plan is to add the feature to attach the debugger to any python process in the future (hopefully near future).

like image 35
Don Avatar answered Nov 13 '22 08:11

Don


You'll find here my .vscode/launch.json setting worked for me on Windows 10/VScode 1.60.0 with main localted in app/main.py :

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python : app.main",
            "cwd": "${workspaceFolder}",
            "type": "python",
            "request": "launch",
            "program": "app/venv/fastapi/Scripts/uvicorn.exe",
            "args": ["app.main:app", "--host=127.0.0.1", "--port=8000", "--reload", "--log-level=error" ],
            "console": "integratedTerminal",
            "postDebugTask": "killdebugger"
        }
    ]
}

and the correponding .vscode/tasks.json to kill the server when debug exit :

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "killdebugger",
            "type": "shell",
            "command": "netstat -nao | grep \"8000\"|awk '{ print $5 }'| xargs kill -9"
        }
    ]
}
like image 43
pamtrak06 Avatar answered Nov 13 '22 08:11

pamtrak06