banner
TerryHu

Terry's Site

bilibili

Debugging remote uwsgi startup code with breakpoints in Pycharm

image

This blog is just to record and sort out my entire exploration process. If you need details, you can comment or contact me, although I guess no one will look at it.

  1. Download Pycharm 2025.1.2 and activate the professional version under the circumstances where the JetBrains official website is blocked.

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

  3. Use the Python interpreter inside the remote container.
    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 in 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 &

Actually, at this step, you can debug a single file remotely. Look for a built-in current file debugging mode in Pycharm's Run/Debug, set breakpoints, and you can debug with breakpoints.

  1. Remote breakpoint debugging of the project.
    Install pydevd-pycharm~=251.26094.141 (the specific version will be prompted when you click on edit configurations in the Python debug server).
    Download tar.gz from pydevd-pycharm·PyPI, extract it using tar -zxvf, install it with python3 setup.py install, and finally 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. The wsgi.py file can be debugged with breakpoints, but after initiating an HTTP request, it does not stop in the URL handling function. The problem manifests as the request seemingly being stuck at the breakpoint, but in fact, Pycharm did not catch it. It 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 glanced at the Python debug server and saw an option for "allow multiple instances," which I also checked, but I don't know 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.