[PYTHON] CPU usage is 100% when using pygame on Ubuntu 16.04

Symptoms

Using pygame on Ubuntu 16.04 results in 100% CPU usage.

main.py


import time
import pygame

pygame.init()

while True:
    time.sleep(1)

It is a code that does nothing, but if you check it on top

top


top - 13:18:43 up  6:21,  1 user,  load average: 0.78, 0.56, 0.48
Tasks: 329 total,   1 running, 328 sleeping,   0 stopped,   0 zombie
%Cpu(s): 26.0 us,  4.8 sy,  0.6 ni, 68.2 id,  0.4 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 12222916 total,  5867808 free,  3502168 used,  2852940 buff/cache
KiB Swap: 12502012 total, 12502012 free,        0 used.  7608068 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     
18635 USER     20   0  704956  31568  18668 S  93.8  0.3   0:41.60 python2.7   
 1800 USER     20   0 1251136 113176  64712 S   6.2  0.9   6:27.81 compiz      
    1 root      20   0  119960   6064   3932 S   0.0  0.0   0:03.00 systemd     
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd    
    3 root      20   0       0      0      0 S   0.0  0.0   0:00.21 ksoftirqd/0 
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:+ 
    7 root      20   0       0      0      0 S   0.0  0.0   0:09.43 rcu_sched   
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh      
    9 root      rt   0       0      0      0 S   0.0  0.0   0:00.09 migration/0 
   10 root      rt   0       0      0      0 S   0.0  0.0   0:00.04 watchdog/0  
   11 root      rt   0       0      0      0 S   0.0  0.0   0:00.04 watchdog/1  
   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.07 migration/1 
   13 root      20   0       0      0      0 S   0.0  0.0   0:00.20 ksoftirqd/1 
   15 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:+ 
   16 root      rt   0       0      0      0 S   0.0  0.0   0:00.04 watchdog/2  
   17 root      rt   0       0      0      0 S   0.0  0.0   0:00.08 migration/2 
   18 root      20   0       0      0      0 S   0.0  0.0   0:00.24 ksoftirqd/2 

You can see that Python occupies the CPU for a terrible time.

The environment is as follows. CPU:Core i3 4005u Memory:12G OS:Ubuntu 16.04(LTS)

Investigation / cause

Apparently pygame.init () is doing something wrong. According to the pygame documentation, pygame.init () seems to be a function that initializes multiple modules at once. Only the module specified by pygame. [module name] .init () can be initialized. So, when I initialized the modules individually, the CPU usage was 100% immediately after calling pygame.base.init (). Is this the cause?

Countermeasures

Instead of writing pygame.init (), if there is a module that needs to be initialized, it will be initialized with pygame. [Module name] .init (). Fortunately, pygame.base doesn't seem to cause an error without initialization, so I solved it for the time being.

Recommended Posts

CPU usage is 100% when using pygame on Ubuntu 16.04
Error around GUI when using PyQt5 on Ubuntu (WSL2)
Troublesome story when using Python3 with VScode on ubuntu
Install Caffe on Ubuntu 14.04 (CPU mode)
Introducing Python using pyenv on Ubuntu 20.04
Preparing python using vscode on ubuntu
Using a serial console on Ubuntu 20.04
Run Yocto on Ubuntu using QEMU.
[Super basic] Docker command procedure manual when using Ubuntu on Mac
Solution when HDMI audio output is not possible on Ubuntu 20.04.1 LTS
Build Python environment on Ubuntu (when pip is not the default)
When the target is Ubuntu 16.04 in Ansible
Error, warning when using TensorFlow on Mac
Control CPU usage of processes (groups) (using cgroups)
A addictive story when using tensorflow on Android
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Python development on Ubuntu on AWS EC2 (using JupyterLab)
Notes for using TensorFlow on Bash on Ubuntu on Windows
Shebang on Ubuntu 20.04
Precautions when using sqlite3 on macOS Sierra (10.12) with multiprocessing
Set up a file server on Ubuntu 20.04 using Samba
I got a UnicodeDecodeError when pip install on ubuntu
Check types_map when using mimetypes on AWS Lambda (Python)
Solution if you crash when using selenium on heroku
SoC FPGA: A small story when using on Linux
Get html source when javascript is enabled using curl
A swampy story when using firebase on AWS lamda
Minimum memo when using Python on Mac (pyenv edition)
Suspend shutdown on linux When sleep is not possible
Minimum notes when using Python on Mac (Homebrew edition)