Django网站占用CPU问题,怎么解决?

Django网站系统,2核4G,2M带宽 启动方式:uwsgi,网站文章页面数量较大,有百万条数据,运行的时候宝塔面板CPU

使用率和负载均衡经常100%满负荷,访问网站也很卡。
即使重启了,也会立即100%。

重启后查看日志显示如下信息:
bytes in 3061 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 0)
Mon Jan 16 21:20:06 2023 - uWSGI worker 1 screams: UAAAAAAH my master disconnected: i will kill myself !!!
*** Starting uWSGI 2.0.20 (64bit) on [Mon Jan 16 21:20:20 2023] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-44) on 13 September 2022 08:36:34
os: Linux-3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017
nodename: iz0jl3vy25352s5incah63z
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /
detected binary path: /www/wwwroot/testwww/testwl_venv/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to /www/wwwroot/testwww
your processes number limit is 14339
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on 0.0.0.0:8097 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:37945 (port auto-assigned) fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
Python version: 3.8.5 (default, Sep 13 2022, 16:20:52)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
Python main interpreter initialized at 0x2663aa0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 166752 bytes (162 KB) for 2 cores
*** Operational MODE: threaded ***
WSGI app 0 (mountpoint='') ready in 9 seconds on interpreter 0x2663aa0 pid: 20506 (default app)
mountpoint  already configured. skip.
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 20506)
spawned uWSGI worker 1 (pid: 20568, cores: 2)
spawned uWSGI http 1 (pid: 20569)
[pid: 20568|app: 0|req: 2/1] 127.0.0.1 () {40 vars in 647 bytes} [Mon Jan 16 21:20:29 2023] GET /bjz/ac-nbcyq-haibeizhou.html => generated 208147 bytes in 7860 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 1)
[pid: 20568|app: 0|req: 2/2] 127.0.0.1 () {38 vars in 687 bytes} [Mon Jan 16 21:20:29 2023] GET /hljs/ca-hgsoo-angrenxian.html => generated 209339 bytes in 10999 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 0)
[pid: 20568|app: 0|req: 3/3] 127.0.0.1 () {40 vars in 646 bytes} [Mon Jan 16 21:20:40 2023] GET /bjz/ac-nbcyq-lingaoxian.html => generated 208434 bytes in 2190 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 1)
[pid: 20568|app: 0|req: 5/4] 127.0.0.1 () {38 vars in 675 bytes} [Mon Jan 16 21:20:43 2023] GET /fjs/ca-lyse-ningan.html => generated 203309 bytes in 6063 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 0)
[pid: 20568|app: 0|req: 5/5] 127.0.0.1 () {40 vars in 643 bytes} [Mon Jan 16 21:20:46 2023] GET /bjz/ac-nbcyq-changzhi.html => generated 209180 bytes in 3193 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 1)
[pid: 20568|app: 0|req: 7/6] 127.0.0.1 () {40 vars in 639 bytes} [Mon Jan 16 21:20:53 2023] GET /bjz/ac-nbcyq-dazhou.html => generated 209600 bytes in 4730 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 0)
[pid: 20568|app: 0|req: 7/7] 127.0.0.1 () {38 vars in 677 bytes} [Mon Jan 16 21:20:53 2023] GET /jss/aa-jyse-pyixian.html => generated 208142 bytes in 6894 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 1)
[pid: 20568|app: 0|req: 9/8] 127.0.0.1 () {40 vars in 643 bytes} [Mon Jan 16 21:21:01 2023] GET /bjz/ac-nbcyq-nanchong.html => generated 209845 bytes in 3764 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 0)
[pid: 20568|app: 0|req: 10/9] 127.0.0.1 () {38 vars in 687 bytes} [Mon Jan 16 21:21:03 2023] GET /fjs/ca-smsi-baixiangxian.html => generated 204189 bytes in 6114 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 1)
[pid: 20568|app: 0|req: 10/10] 127.0.0.1 () {40 vars in 645 bytes} [Mon Jan 16 21:21:08 2023] GET /bjz/ac-nbcyq-guangzhou.html => generated 210219 bytes in 3177 msecs (HTTP/1.0 200) 5 headers in 169 bytes (1 switches on core 0)

网站是不是有 wa 矿程序…

几个原因看下:
1.假如内容数据很大,是不是缓存数据太大导致
2.reload导致;解决参考https://blog.csdn.net/weixin_33743880/article/details/88614441
3.参考配置问题:https://qa.1r1g.com/sf/ask/4037601711/

希望能够帮到你

首先,您需要进行性能调优来确定网站占用CPU的原因。您可以使用工具如top,htop等来查看网站进程的CPU使用率。您可以使用Django Debug Toolbar来查看您网站中各个部分的性能。

如果发现是数据库查询导致的高CPU使用率,可以考虑优化数据库查询或者缓存。

如果是网站中的某些视图或者函数导致的高CPU使用率,可以考虑优化代码或者使用缓存。

如果是网站中的静态文件导致的高CPU使用率,可以考虑使用CDN来加速静态文件的加载。

如果是网站中的某些部分导致的高CPU使用率,可以考虑使用多进程或多线程来优化网站性能。

如果还无法确定原因, 可以尝试使用阿里云或腾讯云等云服务商的监控服务来确定瓶颈所在。

除此之外, 使用负载均衡和集群技术来提高网站的可用性和扩展性也是很有用的。


当前系统配置可能不足以支持网站的正常运行,您可以考虑以下几种解决方案来优化网站性能:

数据库优化:优化数据库查询,使用缓存来减少数据库查询次数。

缓存优化:使用缓存来减少数据库查询次数和视图函数的执行次数。

代码优化:优化代码,减少不必要的计算和循环。

静态文件优化:使用CDN来加速静态文件的加载。

使用负载均衡和集群技术来提高网站的可用性和扩展性。

增加硬件配置,如内存和CPU核心数。

使用云服务商的监控服务来确定瓶颈所在, 并根据监控结果来调整配置。

建议您试着逐一进行优化并监控网站性能, 以找到最佳的解决方案。


这段日志显示您的网站是使用uWSGI来启动的,您的系统配置为2核4G,2M带宽。日志中还提示了一些警告,即您当前uWSGI正在以root权限运行,建议使用--uid选项来更改运行用户。

另外,日志中还提到了一些关于请求的信息,显示您的网站在处理请求时产生了一些延迟,这可能是由于网站数据量较大,导致数据库查询和页面生成等操作较慢。

为了解决这个问题,可以考虑以上我提到的优化方案,如数据库优化,缓存优化,代码优化等。同时,您也可以使用云服务商的监控服务来确定瓶颈所在并进行优化。

  1. 检查是否存在多余的进程,并终止它们。
  2. 检查是否有程序或操作占用大量 CPU 资源,例如某些定时任务等。
  3. 检查是否有大量缓存或日志文件占用磁盘空间,导致 I/O 请求延迟。
  4. 对查询进行优化,减少不必要的查询,减少查询次数。
  5. 重新部署程序,更新代码,以改进性能。
  6. 根据系统负载情况,拓展服务器资源,提高服务器处理能力。

Django网站占用CPU问题可能是由于一些性能瓶颈造成的。可能的解决方案包括:

使用缓存:使用缓存可以减少数据库查询的次数,提高性能。

优化数据库:使用数据库优化工具,如EXPLAIN,ANALYZE等检查和优化数据库的性能。

使用多进程:使用多进程运行Django,可以更好地利用多核CPU的性能。

扩大资源:如果资源不够用,考虑扩大机器配置,如增加内存或者更换大颗粒度的CPU。

增加配置: 使用uwsgi的--processes或--threads参数来增加进程或线程数量。

使用CDN:使用CDN加速静态资源的访问。

检查代码中的性能瓶颈,如频繁的I/O操作,长时间运行的循环等。

使用静态文件服务器,如Nginx,来处理静态文件,让Django更多的关注动态请求。