PDA

View Full Version : PyQt4 app : high CPU usage from python and Xorg



tipote
10th June 2009, 08:02
Dear QtCentre users,

I am writing an application to analyze audio data in realtime. The audio data typically comes from a microphone. "Realtime" here means a lantency lower than 20 ms so that the delay between the capture and the display on screen is not humanly noticeable. The display is made of level meter, scope, spectrum and rolling spectrogram widgets.

I have chosen to use :
-python as a language,
-numpy/scipy for the audio processing (mostly FFT currently),
-pyaudio (portaudio bindings),
and most importantly for this forum audience :
-PyQt4 for the GUI,
-PyQwt for the plot widgets.

To give you an idea, here is a screen capture of what it gives : 3323

I think I have managed to get a working prototype which meet the above goals, apart from a problematic detail : the CPU usage.

The program is written around a QTimer whose period is around 20 ms. Th timer slot computer the FFT when it has enough data, updates the widgets. I profiled the whole thing with cProfile, and worked hard to remove the major hotspots. The rolling spectrogram is made in a manner as clever as possible, and the plot are updated only where they need to. Here is the profile seen by kcachegrind : 3324
As you can see, most of the time is spent in exec_, which is the main loop if I understand correcly.

Still, when I run the program, the fans start to run, and 'top' confirms the high CPU activity :

Tasks: 170 total, 3 running, 167 sleeping, 0 stopped, 0 zombie
Cpu(s): 41.5%us, 4.0%sy, 0.0%ni, 54.2%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
Mem: 2007728k total, 1937212k used, 70516k free, 75384k buffers
Swap: 0k total, 0k used, 0k free, 1025968k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4678 tipote 20 0 946m 71m 36m R 42.7 3.6 0:47.81 python
1876 root 20 0 506m 131m 11m S 40.7 6.7 35:46.64 Xorg
2119 tipote 20 0 430m 6960 5032 S 3.6 0.3 3:14.54 pulseaudio
...

40% by python and 40% by Xorg is not a satisying result, but I have no idea what to do now to improve that situation since I cannot see anymore hotspots in the profile. So here is my question : what can I do to lower that CPU usage ? Can you see some design flaws in the above description ?

Thank you for your help,

Best regards,

Timothée Lecomte, tipote

P.S. : this has been done on Fedora Core 11, which use QT 4.5, PyQT 4.4.4, Qwt 5.1.1 (also tested on Ubuntu 9.04)

tipote
23rd June 2009, 11:09
I guess it could be easier to answer with the code... If you wish to look at it to see what could be wrong, here it is:

http://github.com/tlecomte/friture/tree/master

License-wise, it is GPLv3, and quality-wise, it is still very young, but I think you can get the idea easily by looking at friture.py as an entry point.

Thanks for your help !

stuwilkins
30th January 2012, 17:04
HI!

Sorry to wake up an old thread, but did you ever get this problem fixed? I have noticed the same problem that my applications use 100% cpu usage when I call the update() function from widgets periodically.

S

amleto
30th January 2012, 17:50
have you run a profiler? Just because cpu is at 100% doesn't mean there is a bug with an easy fix. You should consider if you calling update will result in feedback loop of update->update->update etc.