8

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

Greg Enslow
  • 1,438
  • 5
  • 18
  • 28

4 Answers4

18

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

Mauricio
  • 2,552
  • 2
  • 29
  • 43
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).

Don
  • 6,632
  • 3
  • 26
  • 34
2

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"
        }
    ]
}
pamtrak06
  • 21
  • 2
  • 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](/help/how-to-answer). – Community Nov 12 '21 at 16:04
  • 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. – – jahantaila Nov 12 '21 at 20:12
  • i've read you can set an environment variable to allow debugger support for gevents: GEVENT_SUPPORT=True – mike01010 Apr 19 '23 at 00:11
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()
Nikolay Shebanov
  • 1,363
  • 19
  • 33