Reputation: 131
Goal
To track the program real time that user is interacting with.
Expected output
To get the information of current process that user is interacting with.
What I've done
Used psutil
to list all the process and find the process that uses CPU most. But it resulted in returning python.exe, which was using most CPU because it was counting processes.
Question
Is there any other way around to do the task without this kind of mistake?
Or any keyword that I can google for would be nice, too.
Upvotes: 0
Views: 33
Reputation: 104524
I suppose figuring out why your own app itself is using all the CPU would be your first priority. :) My psychic powers suggest that you are polling the system constantly without sleeping. Did you consider sleeping for a half second after enumerating processes?
Using CPU metrics isn't the best way to accomplish what you are after. You didn't mention what OS you are, but if you are on Windows, then you want to be tracking the window in the foreground, as that is what the user is interacting with. By getting the foreground HWND, you can likely map it back to process id and ultimately process name. Not sure about Mac, but I bet there's an equivalent call.
Upvotes: 1