Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does referring to a class in (python) pyglet.image cause heavy cpu load on Windows?

I'm using python's pyglet module (python 3 on Windows). When I refer to any classes within pyglet.image, python's CPU usage jumps up and doesn't drop until I exit python. For example:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
C:\Anaconda3>python.exe
Python 3.4.3 |Anaconda 2.3.0 (64-bit)| (default, Mar  6 2015, 12:06:10) [MSC v.1
600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyglet #No problem!
>>> pyglet.image.ImageData #Heavy CPU load until I exit python
<class 'pyglet.image.ImageData'>

Is this expected behavior? Why does mentioning this class (not even instantiating it) lead to such high CPU load?

Systems I've tested:

  • Windows 7 desktop with Anaconda python 3.4.3 and pyglet installed through 'pip install pyglet': High CPU usage (my problem)

  • The same Win7 desktop with Anaconda python 3.4.3, but pyglet installed through 'pip install hg+https://bitbucket.org/pyglet/pyglet': High CPU usage.

  • The same Win7 desktop with python 3.5 from python.org and pyglet installed through 'pip install pyglet': High CPU usage.

  • Fedora 22 Lenovo laptop with python 3.4.2 and pyglet 1.2.1 installed through dnf: no problem.

  • Windows 10 HP laptop with Anaconda python 3.4 and pyglet installed through 'pip install pyglet': no problem.

Is it possible this is hardware-dependent?

like image 861
Andrew Avatar asked Nov 20 '15 18:11

Andrew


1 Answers

It is probably related to the following lines on the module:

# Initialise default codecs
from pyglet.image import codecs as _codecs
_codecs.add_default_image_codecs()

The order for loading the default codecs is:

# Add the codecs we know about.  These should be listed in order of
# preference.  This is called automatically by pyglet.image.

# Compressed texture in DDS format
try:
    from pyglet.image.codecs import dds
    add_encoders(dds)
    add_decoders(dds)
except ImportError:
    pass

# Mac OS X default: QuickTime
(...)

# Windows XP default: GDI+
(...)

# Linux default: GdkPixbuf 2.0
(...)

# Fallback: PIL
(...)

# Fallback: PNG loader (slow)
(...)

# Fallback: BMP loader (slow)
(...)

Because of lazy loading pyglet.image is only loaded when you refer to something, and you are probably using one of the slow fallbacks. If that is the case maybe you can try installing/uninstalling the codecs so that you use one at a time and find out if the problem is indeed with the codecs. Posting your versions of these codecs may help reproduce the issue.

like image 149
rll Avatar answered Sep 22 '22 21:09

rll