Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High CPU usage in pygame #331

Closed
illume opened this issue Mar 13, 2017 · 50 comments
Closed

High CPU usage in pygame #331

illume opened this issue Mar 13, 2017 · 50 comments

Comments

@illume
Copy link
Member

illume commented Mar 13, 2017

Originally reported by: Maksym Planeta (Bitbucket: mplaneta, GitHub: mplaneta)


Hello,

I have a following snippet of code:

#!python


pygame.init()
import time
time.sleep(50)

When the scripts goes sleep, I observe high CPU utilization.

I tracked this down to sound system, and when I add pygame.mixer.quit() after init, script utilizes CPU as expected. I figured out that pygame creates a new process, which eats up all the CPU and the backtrace of this thread looks as follows (this is how I pointed problem to sound mixer):

(gdb) bt
#0  0x00007f297504554d in poll () at ../sysdeps/unix/syscall-template.S:84
#1  0x00007f295e16107e in ?? () from /usr/lib/x86_64-linux-gnu/libasound.so.2
#2  0x00007f295e165fb8 in ?? () from /usr/lib/x86_64-linux-gnu/libasound.so.2
#3  0x00007f295e1a60c8 in ?? () from /usr/lib/x86_64-linux-gnu/libasound.so.2
#4  0x00007f29748c9fc5 in ALSA_PlayAudio (this=0x5633de70d990) at ./src/audio/alsa/SDL_alsa_audio.c:321
#5  0x00007f297489ed90 in SDL_RunAudio (audiop=audiop@entry=0x5633de70d990) at ./src/audio/SDL_audio.c:215
#6  0x00007f29748a6f58 in SDL_RunThread (data=0x5633de686360) at ./src/thread/SDL_thread.c:204
#7  0x00007f29748e79d9 in RunThread (data=<optimized out>) at ./src/thread/pthread/SDL_systhread.c:47
#8  0x00007f2975e5a424 in start_thread (arg=0x7f295622e700) at pthread_create.c:333
#9  0x00007f297504e9bf in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:105

Python version: 3.5.3
libasound2 version: 1.1.3-4 (Debian testing)


@illume
Copy link
Member Author

illume commented Mar 13, 2017

Original comment by Thomas Kluyver (Bitbucket: takluyver, GitHub: takluyver):


I can reproduce this if I install from a wheel from PyPI, but not if I install pygame from source. That points to some error with the packaging - maybe an issue with the old ALSA library that it's built with. Possibly we need to try building a newer version of ALSA from source.

@illume
Copy link
Member Author

illume commented Mar 18, 2017

Original comment by Thomas Kluyver (Bitbucket: takluyver, GitHub: takluyver):


I was hoping building with a newer version of ALSA (pull request https://bitbucket.org/pygame/pygame/pull-requests/77/build-alsa-lib-ourselves-in-docker-base/diff) would fix this, but it does not seem to, unfortunately. I'm not sure what else to try.

@illume illume added this to the 1.9.x milestone Mar 26, 2017
@astronerdnj
Copy link

Hi there,

I have exactly the same problem running Pygame 1.9.3 with Python 3.5.2 under Ubuntu 1.6.04 LTS. All I have to do is pygame.init() and the CPU utilization shoots up drastically. I tried following this with pygame.mixer.quit() but it made no difference. I believe I installed Python using PIP3.

Just wondering if you have made any progress on the issue and/or have any ideas on what I should do?

Thanks

@takluyver
Copy link
Member

I think you can work round it by building pygame yourself from source. I haven't made any progress in working out what the problem is.

@davidshumway
Copy link

Noticing this behavior as well: Roland00111/Trumpocalypse#18.

@steckelj
Copy link

I also have this behavior on a pi, however the pygame.mixer.quit() does not resolve the problem. Does anyone have a good link on building from source on raspbian?

@davidshumway
Copy link

@steckelj One solution was maybe to not use pygame.init(). Are you using this? If so, try not doing so. Instead, do pygame.___.init() for any modules that are required. Such as pygame.display.init() and pygame.mixer.init() and pygame.font.init(). If you are interested see https://github.com/Roland00111/Trumpocalypse/blob/master/UI/Trumpocalypse.py for implementation.

@steckelj
Copy link

I am actually only using pygame.display.init(). I read many guides on the high CPU utilization before I posted on the issue here, and I have no loop and am just sitting at pygame.event.wait() with no allowed events and have near 100% CPU. No other updating. Is it possible I'm not experiencing the same issue?

I'll have to try a test pygame.display.init() only and see if I get the same thing.

I did find what looks to be good instructions for building from source, see pygame section here: http://elinux.org/RPi_Debian_Python3
(I have yet to test the instructions)

@davidshumway
Copy link

Okay. Yes, for us it was similar. One solution was to use pygame.time.wait(0) in the while loop. See here: https://github.com/Roland00111/Trumpocalypse/blob/master/UI/eventsloop.py.

        while event_loop:
            self.process_pygame_events()
            pygame.time.wait(0)

Where process_pygame_events() is a function where pygame.event.get() is looped through.

Another solution may be related to clock tick() or fps.

@steckelj
Copy link

I had been through all of that above - definitely thought it was me for two weeks before I arrived here at this bug and read through everything I could find. I actually eliminated any loop entirely to be sure it wasn't that.
However, I will better confirm I am experiencing this issue before I post anything further here.

@steckelj
Copy link

steckelj commented May 19, 2017

I believe I can now confirm this is happening even if you only init the display. Can anyone suggest a previous version I can use? Will that avoid the problem?

The following code produces near 100% CPU on a Pi:

import pygame
pygame.display.init()
pygame.event.set_allowed(None)
pygame.event.wait()

Adding the pygame.mixer.quit() also doesn't help (I didn't init it anyhow). I also tried removing pygame and compiling from source via the following but still experience the same issue. The instructions below specified that sound had been removed.

GET PYGAME SOURCE CODE
sudo apt-get install mercurial
hg clone https://bitbucket.org/pygame/pygame
cd pygame

INSTALL DEPENDENCIES
sudo apt-get install libsdl-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev
sudo apt-get install libsmpeg-dev libportmidi-dev libavformat-dev libswscale-dev

BUILD AND INSTALL PYGAME
python3 setup.py build
sudo python3 setup.py install

Any help to get around this for the moment would be much appreciated!

@takluyver
Copy link
Member

I think there are a couple of different bugs in play here; the original one reported appeared to be only an issue with the pre-built wheels, and went away when I built pygame from source on the target system. The new one doesn't. Unfortunately I don't have any recommendations for either right now.

@Duality4Y
Copy link

I observed high cpu load too, from the package installed via pip,
but when i installed from source it was all fine.

@nylocx
Copy link

nylocx commented Dec 15, 2017

Hi I have the same issue on Arch-Linux and I can fix it with not initializing the mixer module.
Some Version info that could be important:
Python 3.6.3
pygame==1.9.3 (from pip! It works with the arch packaged pygame 1.9.3)
sdl 1.2.15-9
portmidi 217-5
sdl_image 1.2.12-4
sdl_ttf 2.0.11-4
sdl_mixer 1.2.12-5

@VladMasarik
Copy link

VladMasarik commented Jan 21, 2018

Hey there I seem to have same problem.
I used
python3 -m pip install pygame --user
to install pygame as said on pygame website.

import time
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
from pygame import mixer
mixer.init()
time.sleep(10)
mixer.quit()

CPU usage jumps to 90% after mixer.init() and decreases right after mixer.quit()
Off-topic... any Idea why I don't have /usr/local/lib/python2.7/dist-packages in sys.path by default and so I have to append it explicitly before I can use pygame?

@illume
Copy link
Member Author

illume commented Feb 13, 2018

I noticed this issue with fluidsynth and pulseaudio. Worth investigating to see if this is the cause: FluidSynth/fluidsynth#338

@illume
Copy link
Member Author

illume commented Feb 20, 2018

Another "high cpu" issue on Ubuntu launchpad, with pulse audio in the traceback https://bugs.launchpad.net/ubuntu/+source/pygame/+bug/1314377

@illume
Copy link
Member Author

illume commented Feb 27, 2018

This is a fluidinfo/pulseaudio/SDL_mixer issue, and best addressed there.

We have the option of not including fluidsynth, but then music wouldn't work for some people.
App side, a work around of using alsa SDL audio driver with an environment variable, or not initialising the music mixer if not using it.

@kooscode
Copy link

kooscode commented Dec 9, 2018

same issue with python 3.6.3 and installed with pip3.

100% CPU pegged with this code..

import pygame
import time

pygame.init()

while True:
    time.sleep(1)

but works perfect when calling pygame.mixer.quit like so..

import pygame
import time

pygame.init()
pygame.mixer.quit()

while True:
    time.sleep(1)

@SebastianJL
Copy link

I appear to have the same problem.
2 cores have 100% usage if I use:

pygame.init()

The cpu usage drops down to about 20% on only one core if i use:

pygame.init()
pygame.mixer.quit()

@mcpalmer1980
Copy link
Contributor

I'm quite surprised to see such a major issue unchanged after so long. I started using pygame about 6 months ago but stuck with the 1.9.1 version included with Linux Mint 18.3 because of this performance issue. At the time I suspected it was blitting or some such thing that drove my CPU usage up to 20%. As I neared the release of my pygames, I did more testing and found that 1 of my 8 cores is at 100% despite the blitting speed being higher in 1.9.4.

Calling mixer.quit() does alleviate the problem, but neither silence nor rapid battery drain are acceptable so I must stick with 1.9.1. Perhaps changing the driver to ALSA in an environment variable would work as one user suggested, but he gave no instructions. The increased blitting speed I measured i 1.9.4 is completely wasted because the recent versions are broken in Linux.

The following measurements were taken from the tottime column after running the following command: 'python2 -m cProfile run_game.py >profile.txt'. The longer time spent in pygame.clock.tick() suggests to me that pygame's efficiency significantly increased. All my physics, collision, AI and other logic uses less than 0.5% of the total time according to cProfile.

	1.9.1	1.9.4
CPU use	7%	20% -> ouch, bye-bye battery and a hot lap
tick()	59.5%	85.6%
blit()	29.4%	9.4%
flip()	11.1%	5%

@s0lst1ce
Copy link
Contributor

s0lst1ce commented Feb 20, 2019

Hey I have the same issue. When I start pygame, whatever the code I'm running it uses one of my core at 100%.

I'm using arch with Gnome (also tested on Linux Mint 17 cinnamon and on Windows 10) & pygame (1.9.4) as well as python 3.7.2. I've always installed pygame with pip. I've also tried to run my game with previous version of pygame (1.9.3 & 1.8) and python (3.5) and I always get the same deceiving results.

A working hack for me is to quit the mixer after initializing pygame. Instead of 8.33% CPU (one full core) I get 0.2% of CPU usage from my game. However I'd like to add some sound in my game and wonder how I could do this while keeping acceptable performance.
If you need any additional information or help to try out things don't hesitate to ask.

@mcpalmer1980
Copy link
Contributor

mcpalmer1980 commented Mar 6, 2019 via email

@e4e
Copy link

e4e commented Jun 14, 2019

seems finally fixed in version: pip install "pygame>=2.0.0.dev1"
🎉
PS: running Linux 4.15.0-51-generic 16.04.1-Ubuntu
pygame 2.0.0.dev1 (SDL 1.2.15, python 2.7.12)

@notpygame
Copy link
Contributor

Hi,

can someone with the issue on Linux please try out the latest pre-release?
It uses SDL2, and a different audio backend.

python3 -m pip install pygame==2.0.0.dev2 --user

cheers!

@s0lst1ce
Copy link
Contributor

Works for me. Good job !

@mobariza
Copy link

mobariza commented Jun 21, 2019

Thank's. But does not work for me, one CPU is still on 100%.
This is my code (included the hints since 2017):

import pygame
from pygame.locals import *
import time

def run_game():
	pygame.init();
	pygame.mixer.quit();
	screen = pygame.display.set_mode((400, 100));
	clock = pygame.time.Clock();
	clock.tick(1);
	# Main loop for the game
	while True:
		# time.sleep(1);
		# if you comment out the sleep and comment out the following: no cpu problem!
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				sys.exit(5);
		pygame.display.flip();
run_game()```

The output is
```pygame 2.0.0.dev2 (SDL 2.0.9, python 3.6.8)
Hello from the pygame community. https://www.pygame.org/contribute.html

@s0lst1ce
Copy link
Contributor

Well you quit the mixer when you've just initialized pygame. Are you sure this is related to the mixer ? To me it sounds like the problem is due to the clock.tick(1) being outside your while loop Thus it will try to run as many times as possible. I think this is the issue. After trying and correcting this code using this advice it ran fine on my computer @mobariza

@notpygame
Copy link
Contributor

@mobariza Adding a clock.tick(60) into the loop should fix that so it can only run 60fps.

@mobariza
Copy link

@notpygame This works fine, thank you a lot!
Anyway a suggestion: This is a hard-to-find error for pygame-newbies like me; the solution does not work if the tick() is outside the while neither if it is inside the for-loop. Maybe a good idea to set the variable by default to a reasonable value, user won't burn her finger on the laptop by trying the most simple pygame-excercise?
By the way, I am not able to raise the one CPU to 100% by any setting of the clock, even by a setting clock.tick(1.000.000.000.000.000)(without dots), it is then still near 40%; maybe still a deeper problem around this?

@s0lst1ce
Copy link
Contributor

s0lst1ce commented Jun 21, 2019

@mobariza If you need more help you should probably ask it on the discord server where many will be glad to help you. Using this unrelated issue as a discussion thread really isn't the best idea ;). Also the docs clearly explain how clock.tick() works. Why would you want to use 100% of your core ?

@mobariza
Copy link

@NotaSmartDev Thank you, my problem is completely solved, thanks to the help of notpygame. And sorry for the misunderstanding. Hunting bugs, I personally appreciate always hints, and as such my test with large numbers to tick() was meant. Nobody wants a 100% CPU usage; but if the setting to an extraordinary high value results in a lower usage as no value set, seems to be at least strange and worth for further examination.
And sorry for the suggestion to set hardcoded a reasonable value to avoid the 100% CPU usage, what nobody wants, including me and all the others coding the first lines with pygame. I thought the one who fixed the bug (notpygame) was a good address to tell.

@mcpalmer1980
Copy link
Contributor

mcpalmer1980 commented Jul 13, 2019 via email

@illume
Copy link
Member Author

illume commented Jul 13, 2019

@mcpalmer1980 did you try this?

```python3 -m pip install --pre "pygame>=2.0.0.dev1" --user `


Edit: pygame 2 has been released now.

python3 -m pip install pygame

For the best up to date install instructions see: https://www.pygame.org/wiki/GettingStarted


Otherwise I think you can install from source... http://www.pygame.org/wiki/CompileUbuntu#pygame%20with%20sdl2%20(alpha)

@nurettin
Copy link

I confirm this as being fixed. High CPU usage when I do pygame.init() is now gone on pygame 2.0.0.dev3.

@flaschbier
Copy link

@notpygame still > 35% cpu with pygame 1.9.6 on a Mac and clock.tick(60). So why do you close this issue?

@s0lst1ce
Copy link
Contributor

Because as it was mentioned this is only fixed with pygame2 @flaschbier

@ghost
Copy link

ghost commented Oct 15, 2019

@steckelj One solution was maybe to not use pygame.init(). Are you using this? If so, try not doing so. Instead, do pygame.___.init() for any modules that are required. Such as pygame.display.init() and pygame.mixer.init() and pygame.font.init(). If you are interested see https://github.com/Roland00111/Trumpocalypse/blob/master/UI/Trumpocalypse.py for implementation.

Worked for my case.

@ravish0007
Copy link

This one helped a lot, Incredible !

@k0mmsussert0d
Copy link

Unfortunately, I'm experiencing this behavior on 2.0.0.dev6 version :(

@s0lst1ce
Copy link
Contributor

s0lst1ce commented Apr 13, 2020

@k0mmsussert0d are you sure your code uses correct pygame logic? For more help I would advise to follow my previous comment recommendations. Otherwise seeing your code would be essential in order to help you.

bitstuffing pushed a commit to lemoncrest/x-pi-one-launcher that referenced this issue Sep 2, 2020
bitstuffing added a commit to lemoncrest/x-pi-one-launcher that referenced this issue Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests