banner
TerryHu

Terry's Site

bilibili

Debugging code started by remote uwsgi with breakpoints in Pycharm

image

This blog is just to record and organize my entire exploration process. If you need details, feel free to comment or contact me, although I guess no one will read it.

  1. Download Pycharm 2025.1.2 and activate the professional version under the restriction of the JetBrains official website.

  2. Ensure local and remote file synchronization
    Use pycharm->deployment for a one-time setup.

  3. Use the Python interpreter inside the remote container
    Because many libraries are installed in the container image, you can connect to the container via SSH in Pycharm and use the Python interpreter inside the container. To connect to the interpreter inside the container, you need to install sshd in the container.

yum install install openssh-server
vim /etc/ssh/sshd_config (change PermitRootLoging to yes)
/usr/sbin/sshd -D &

At this point, you can actually debug a single file remotely. Look for a built-in current file debug mode in Pycharm's Run/Debug section; you can set breakpoints for debugging.

  1. Remote breakpoint debugging for the project
    Install pydevd-pycharm~=251.26094.141 (the specific version will be prompted when you click edit configurations in the Python debug server).
    Download tar.gz from pydevd-pycharm·PyPI, extract it using tar -zxvf, and install it with python3 setup.py install. Finally, you can verify with pip3 list.

Add code at the entry point of the project code, fill in the local IP, and any port that does not conflict.

import pydevd_pycharm
pydevd_pycharm.settrace('1.2.3.4', port=50010, stdoutToServer=True, stderrToServer=True)

First, start the local Python debug server, which will prompt you with "Waiting for process connection...". Then start the remote project; I am using uwsgi --ini platform.ini, and uwsgi will call the wsgi.py file. My import pydevd_pycharm is added at the beginning of this file.

However, I encountered a problem: I could set breakpoints in the wsgi.py file, but after making an HTTP request, it did not stop in the URL handling function. The issue manifested as the request seemingly being stuck at the breakpoint, but in fact, Pycharm did not catch it. This is likely because I configured uwsgi for multi-process handling of high-concurrency requests. I then changed the uwsgi configuration to:

[uwsgi]  
http=0.0.0.0:9000  
processes = 1  
threads = 1  
master = false  
vacuum=true  
enable-threads=true  
chdir=/xx/xxx
module=xxxx.wsgi

I noticed there was an "allow multiple instances" option in the Python debug server, which I also checked, but I am not sure if this option is relevant.

Finally, it fucking works!

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.