Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyGame full core usage in simple loop

Tags:

python

pygame

Edit

This is an open issue and appears to be related to alsa audio.

A workaround is to shutdown the audio mixer, or install pyGame from source.

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

I am just beginning development with pyGame and have found that I should use the following to gate CPU time:

fps = 30
clock = pygame.time.Clock()

while True:
    # Logic...
    clock.tick(fps)

The issue that I seem to be having, is that the most basic draw a square program running at 1 FPS consumes a full CPU core.

import pygame

pygame.init()

size = ( 16, 16 )
screen = pygame.display.set_mode(size)
pygame.display.set_caption("High CPU")

clock = pygame.time.Clock()

run=True
while run:

    print("Rendering...")

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             run=False

    screen.fill((255,255,255))

    # Render
    pygame.display.flip()
    clock.tick(1)

# When done
pygame.quit()

As soon as I invoke this script, a single core of my machine spikes to 100%, although the messages are only being printed to the console at the expected 1 FPS update rate.

$ ps aux | grep python
mclark   25867 97.4  0.1 652232 29088 pts/0    Sl+  13:10   0:06 python highCPU.py

And running latest pyGame

$ pip show pygame
Name: pygame
Version: 1.9.3
Summary: Python Game Development
Home-page: http://www.pygame.org
Author: Pete Shinners, Rene Dudfield, Marcus von Appen, Bob Pendleton, others...
Author-email: [email protected]
License: LGPL
Location: /home/mclark/.local/lib/python2.7/site-packages
Requires: 

Is this a possible bug in the latest version of pyGame? Or am I managing time incorrectly?

like image 431
Matt Clark Avatar asked Sep 11 '17 17:09

Matt Clark


1 Answers

We fixed this in pygame 2. It does not take 100% of a CPU core on some linux systems anymore.

python3 -m pip install pygame --upgrade

As of pygame 2.0.0, it may be better to install pygame from source on some linux systems.

cheers,

like image 136
René Dudfield Avatar answered Nov 04 '22 03:11

René Dudfield