wx.adv.Joystick high CPU usage

Hi All,

Windows 10 & 11, python 3.13, wx 4.2.2.

I have been using the joystick library in wx.adv.Joystick for menu navigation in an emulator front-end I have which I believe is a wx widgets wrapper ?? as there isn’t anything within it that does any real work. Anyway, just invoking the below causes around 15% CPU usage until closed. I’m assuming whatever library this is calling is the problem.

joystick = wx.adv.Joystick(joystick=wx.JOYSTICK1)

This isn’t great at all and I’d assume a real problem for low spec machines

I cant use the alternate ‘inputs’ library as it has serious blocking issues - I did try to use that from a thread which is fine but it still blocks that thread from closing while it waits for a joystick event. Some people have tried to work on this issue and there is a ‘fix’ for WIN but not for LINUX and as my application is cross-platform I really need a better solution.

Has anyone come across this and / or have any suggestions

I have a Microsoft X-Box 360 pad rather than an actual joystick, but the Joystick example in the wxPython Demo does recognise it.

The example responds to movement of the pad’s left joystick and 10 of its buttons.

I tested it using wxPython 4.2.2 gtk3 (phoenix) wxWidgets 3.2.6 + Python 3.12.3 + Linux Mint 22 and ran the htop utility to monitor the cpu usage.

If I keep moving the left stick, the cpu usage will go up to 7 to 13%.

When I stop moving the stick it drops to 0%.

I’m not seeing a continuous 15% cpu usage when idle.

This does seem to be a Windows related issue. The demo in Windows is definitely pulling between 15 and 19% CPU load. Just creating a very basic wx window and adding the lines below is enough to cause the problem. Joystick is working just fine but the CPU load is very strange.

self.joystick = wx.adv.Joystick()
if self.joystick.IsOk():
self.joystick.SetCapture(self)

Does changing the value of the SetCapture() method’s pollingFreq argument have any effect?

Edit: Looking in the wxWidgets C++ source code, both the Unix version and the Windows version use a separate thread to read the joystick’s values in the background.

The Unix version (src/unix/joystick.cpp) uses the select() function to wait for changes to the joystick’s values. The polling frequency is used to set the select() function’s timeout parameter.

The Window’s version (src/msw/joystick.cpp) simply uses the polling frequency as the duration of a Sleep() function call in a while-loop. If the polling frequency is set to the default value of zero, it means the while-loop in the thread just keeps looping without pausing.

Caveat: my C++ is very rusty!

Yup, well done - that makes a massive difference. The other library I tried (inputs) does it in a pretty similar way and they also had an issue with CPU load until someone injected a 1ms sleep in the loop just to slow it down a tad. I never realised wx.adv.joystick had this built-in.

Cheers for the help on this!!