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

5

10

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

Bolshevist answered 4/10, 2016 at 0:25 Comment(1)
How does your launch.json config looks like?Banner
S
5

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).

Suzy answered 4/10, 2016 at 9:3 Comment(1)
is this now called debugpy?Commonality
B
22

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

Banner answered 9/3, 2020 at 21:57 Comment(3)
Works great, thank you. If I click STOP the debuger TWICE the program exits without the killdebugger.Mackenie
hi bro this works like charm but I want to export an env variable file before starting this, how to do it?Allomerism
@TalkisCheapShowmeCode. You need to set the "envFile": "path/to/the/file.env".Banner
S
5

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).

Suzy answered 4/10, 2016 at 9:3 Comment(1)
is this now called debugpy?Commonality
B
3

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"
        }
    ]
}
Bandylegged answered 12/11, 2021 at 15:1 Comment(3)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Rainstorm
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. –Blintz
i've read you can set an environment variable to allow debugger support for gevents: GEVENT_SUPPORT=TrueVaporish
D
0

I've experienced the same issue and solved it by following the accepted answer described here: Is it possible to run Falcon app from Python?

As gunicorn forks your process, intercepting it with the VScode debugger is not so simple. The easiest way to do so would be calling your API directly with python

from wsgiref import simple_server
import os
import falcon

app = falcon.API()

if __name__ == '__main__':
    with simple_server.make_server('', int(os.getenv('PORT', 5000)), app) as httpd:
        httpd.serve_forever()
Dictionary answered 9/3, 2021 at 15:26 Comment(0)
T
0

If running Django with Gunicorn do this:

In your project root folder, under .vscode create a launch.json file and add this:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Gunicorn: Django ",
      "type": "debugpy",
      "request": "launch",
      "program": "${workspaceFolder}/venv/bin/gunicorn", # path to virtualenv
      "gevent": true,
      "cwd": "${workspaceFolder}/src", #path to Django project
      "args": [
        "change_me.wsgi:application", # change_me to the directory where wsgi.py is located
        "--bind=127.0.0.1:8080", # change the port as needed
        "--reload",
        "--worker-class=eventlet",
        "--workers=1",
        "--threads=1",
        "--timeout=1800",
        "--reload-extra-file=${workspaceFolder}/src/path_to/templates/base.html", # path to each file to watch
      ]
    },
  ]
}

Install these dependencies with pip:

pip install gunicorn django eventlet gevent
Ticino answered 11/8, 2024 at 2:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.