-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Comments
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. |
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. |
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 |
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. |
Noticing this behavior as well: Roland00111/Trumpocalypse#18. |
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? |
@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. |
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 |
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.
Where process_pygame_events() is a function where pygame.event.get() is looped through. Another solution may be related to clock tick() or fps. |
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. |
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 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 INSTALL DEPENDENCIES BUILD AND INSTALL PYGAME Any help to get around this for the moment would be much appreciated! |
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. |
I observed high cpu load too, from the package installed via pip, |
Hi I have the same issue on Arch-Linux and I can fix it with not initializing the mixer module. |
Hey there I seem to have same problem. 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 |
I noticed this issue with fluidsynth and pulseaudio. Worth investigating to see if this is the cause: FluidSynth/fluidsynth#338 |
Another "high cpu" issue on Ubuntu launchpad, with pulse audio in the traceback https://bugs.launchpad.net/ubuntu/+source/pygame/+bug/1314377 |
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. |
same issue with python 3.6.3 and installed with pip3. 100% CPU pegged with this code..
but works perfect when calling pygame.mixer.quit like so..
|
I appear to have the same problem.
The cpu usage drops down to about 20% on only one core if i use:
|
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.
|
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. |
My games run at about 8% cpu with sound. They use pygame 1.9.1 on python
2.7 or 3.5. Pygame 1.9.1 is the version included with Linux Mint 18.3. Not
sure where else you can get it.
Check out Cooties and Flyboy. The sourcce code is not available there but I
can send it if you're interested
https://mcpalmer1980.itch.io/ <https://mcpalmer1980.itch.io/>
…On Wed, Feb 20, 2019 at 8:44 AM 01g1n ***@***.***> wrote:
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) & pygame
(1.9.4) as well as python 3.7.2. I've also tried to run my game with
previous version of pygame and python 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.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#331 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/As7LaF7LrD9Dcp_oBAyIRYvZiHfRzdk0ks5vPVFbgaJpZM4NRY8j>
.
|
seems finally fixed in version: |
Hi, can someone with the issue on Linux please try out the latest pre-release?
cheers! |
Works for me. Good job ! |
Thank's. But does not work for me, one CPU is still on 100%.
|
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 |
@mobariza Adding a clock.tick(60) into the loop should fix that so it can only run 60fps. |
@notpygame This works fine, thank you a lot! |
@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. |
I would love to try out the pygame2-dev on my Linux Mint Mate 18.3 machine
but I cannot. When trying to install with pip I get a sdl-config failure. I
tried to install libsdl-dev but apt cannot resolve dependencies. There
doesn't seem to be an libsdl2-dev package in my distribution, or perhaps
libsdl-dev includes sdl2.
I'm eagerly waiting to try pygame2 the instant somebody builds a compatible
package. In the meant time pygame1.9.1 on python2 is working fine for my
projects except for the damn joystick debug info printing to my console 300
times per second. A problem fixed in newer versions that include the bug
discussed on this thread.
…On Thu, Jun 20, 2019 at 10:38 AM notpygame ***@***.***> wrote:
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!
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#331?email_source=notifications&email_token=ALHMW2E5DCDSCDCQUHEPPJ3P3OI4VA5CNFSM4DKFR4R2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYFTQXA#issuecomment-504051804>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ALHMW2EFH6HZ6XV2YGSIG2LP3OI4VANCNFSM4DKFR4RQ>
.
|
@mcpalmer1980 did you try this?
Edit: pygame 2 has been released now.
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) |
I confirm this as being fixed. High CPU usage when I do |
@notpygame still > 35% cpu with pygame 1.9.6 on a Mac and |
Because as it was mentioned this is only fixed with pygame2 @flaschbier |
Worked for my case. |
This one helped a lot, Incredible ! |
Unfortunately, I'm experiencing this behavior on |
@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. |
Originally reported by: Maksym Planeta (Bitbucket: mplaneta, GitHub: mplaneta)
Hello,
I have a following snippet of code:
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):
Python version: 3.5.3
libasound2 version: 1.1.3-4 (Debian testing)
The text was updated successfully, but these errors were encountered: