Windows 7 error message

694 views
Skip to first unread message

Mike Driscoll

unread,
Oct 5, 2010, 9:02:42 PM10/5/10
to psutil
Hi,

When I try to use psutil's "get_cpu_percent" method on Windows 7 (both
32 and 64-bit), I get the following traceback:

psutil.error.AccessDenied:
File "C:\Users\Mike\Documents\My Dropbox\Scripts\windows\proc_list
\proc_list.py", line 32, in <module>
cpu_percent = proc.get_cpu_percent()
File "c:\Python25\Lib\site-packages\psutil\__init__.py", line 305, in
get_cpu_percent
user_t, kern_t = _platform_impl.get_cpu_times(self.pid)
File "c:\Python25\Lib\site-packages\psutil\_psmswindows.py", line 69,
in wrapper
raise AccessDenied


I tried with both Python 2.5 and 2.6 as well with no change. Does
anyone know if there's a way to get this to work? Thanks!

- Mike

Dirk Rothe

unread,
Oct 6, 2010, 5:23:20 AM10/6/10
to psutil
I've seen such a problem if the psutil code was not executed in the
main-thread. Maybe this helps.

--dirk

Jay Loden

unread,
Oct 6, 2010, 9:56:58 AM10/6/10
to psu...@googlegroups.com
This can happen if the process isn't fully initialized. If the process was just started, the full process info isn't available yet, and Windows returns an access error. Unfortunately, there's no separate error code for this so we can't tell the difference between an actual access error and a process that hasn't initialized yet.

Other possibilities - could it be a legitimate access error? What kind of user are you running as? If you don't have process query rights as that user then you won't be able to access information for processes owned by other users. We do set SEDEBUG privileges for psutil if possible but that can only work if you've got some enough rights to set that privilege. Can you try reading the CPU info for the current process, i.e. os.getpid() and see if that's working? Ex: 

   python -c "import psutil,os; print psutil.Process(os.getpid()).get_cpu_percent()"

-Jay

Mike Driscoll

unread,
Oct 6, 2010, 2:59:13 PM10/6/10
to psutil


On Oct 6, 8:56 am, Jay Loden <jlo...@gmail.com> wrote:
Well, I'm definitely an admin on my 32-bit version, although I can't
say if the python script is running with elevated privileges or not.
I'm using the script I wrote about here:

http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all-running-processes-with-python/

It's near the bottom of the article. I tried running cmd.exe as
Administrator and then ran my script, but I guess that doesn't change
anything since python continued to give me this error.

I am guessing the UAC is blocking it somehow. Just not sure how to get
around that without turning UAC off.

- Mike

Jay Loden

unread,
Oct 6, 2010, 4:07:40 PM10/6/10
to psu...@googlegroups.com
On Wed, Oct 6, 2010 at 2:59 PM, Mike Driscoll <kyos...@gmail.com> wrote:
Well, I'm definitely an admin on my 32-bit version, although I can't
say if the python script is running with elevated privileges or not.
I'm using the script I wrote about here:

http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all-running-processes-with-python/

It's near the bottom of the article. I tried running cmd.exe as
Administrator and then ran my script, but I guess that doesn't change
anything since python continued to give me this error.

I am guessing the UAC is blocking it somehow. Just not sure how to get
around that without turning UAC off.

- Mike

Did you get a chance to try the example against just the current process itself? Are you getting access denied errors for every process, ones owned by other users, or just having them crop up periodically while looping the process list? That would help narrow things down.

Giampaolo has a Windows 7 box which I unfortunately do not, so maybe he will have some more helpful tips. 

-Jay 

Giampaolo Rodola'

unread,
Oct 7, 2010, 4:30:56 PM10/7/10
to psutil
I've tried this right now on a Windows 7 64-bit as an Administrator
user and I've been able to replicate the problem.
The psutil error seems to be legitimate as also procexp isn't able to
retrieve CPU % statistics for the exact same processes where psutil
fails as well.

The code:

import os, psutil

for p in psutil.process_iter():
try:
p.get_cpu_percent()
except psutil.AccessDenied:
print p.pid, p.name

The output:

4 System
268 smss.exe
348 csrss.exe
396 wininit.exe
408 csrss.exe
448 winlogon.exe
476 services.exe
492 lsass.exe
500 lsm.exe
620 svchost.exe
680 VBoxService.exe
708 svchost.exe
764 svchost.exe
892 svchost.exe
924 svchost.exe
988 svchost.exe
344 svchost.exe
360 svchost.exe
1256 svchost.exe
1536 svchost.exe
1944 wmpnetwk.exe
2392 WmiPrvSE.exe
1652 mmc.exe


I can't think of any work around except maybe trying to run the python
process as SYSTEM user, but I doubt that as well considering that
procexp fails.
It's likely that those processes are somewhat "special" and not
supposed to be accessed in the first place because I've often seen
psutil failing with a permission error also when retrieving other
information.

Hope this helps,


Giampaolo

Dirk Rothe

unread,
Oct 7, 2010, 4:49:51 PM10/7/10
to psutil
Here the output of a main-thread/other-thread version.

The code:
import os, psutil, threading

def run():
for p in psutil.process_iter():
try:
p.get_cpu_percent()
except psutil.AccessDenied:
print p.pid, p.name

print 'main thread:'
run()

print '\nthreaded:'
t = threading.Thread(target=run)
t.start()
t.join()

The output:
main thread:
4 System

threaded:
4 System
368 smss.exe
476 csrss.exe
552 wininit.exe
576 csrss.exe
600 services.exe
612 lsass.exe
620 lsm.exe
736 svchost.exe
796 ibmpmsvc.exe
836 nvvsvc.exe
876 svchost.exe
928 MsMpEng.exe
988 svchost.exe
388 svchost.exe
496 svchost.exe
1092 svchost.exe
1188 WUDFHost.exe
1240 winlogon.exe
1352 svchost.exe
1512 nvvsvc.exe
1548 upeksvr.exe
1580 spoolsv.exe
1784 svchost.exe
1848 svchost.exe
1156 TPHKSVC.exe
1684 AcPrfMgrSvc.exe
1672 AEADISRV.EXE
1900 svchost.exe
2004 PresentationFontCache.exe
2140 mdm.exe
2344 nvPDsvc.exe
2424 svchost.exe
2496 XAudio64.exe
2548 AcSvc.exe
2192 SvcGuiHlpr.exe
2208 SearchIndexer.exe
1332 SUService.exe
3404 wmpnetwk.exe
13976 fb_inet_server.exe
22120 SearchProtocolHost.exe
15596 svchost.exe

Dirk Rothe

unread,
Oct 7, 2010, 4:51:31 PM10/7/10
to psutil
Ah, that was win7-x64, Administrator as well.

--dirk

Giampaolo Rodola'

unread,
Oct 8, 2010, 1:08:13 PM10/8/10
to psutil
Good to know threaded processes have such limitation.
I don't get why I get a lot of access denied exceptions by using a
single thread while you're not.

Jay Loden

unread,
Oct 8, 2010, 1:23:39 PM10/8/10
to psu...@googlegroups.com
I'm not sure what to make of that, other than perhaps the threads are running in a different security context, but I am not sure why that would be the case. Maybe the SEDEBUG privileges aren't extending to the child threads? It's hard for me to do much more than guess since I don't have Win7 but we can log a bug and investigate, along with why Giampaolo's having access denied errors without threads at all.

-Jay

Mike Driscoll

unread,
Oct 9, 2010, 3:43:48 PM10/9/10
to psutil


On Oct 6, 3:07 pm, Jay Loden <jlo...@gmail.com> wrote:
> On Wed, Oct 6, 2010 at 2:59 PM, Mike Driscoll <kyoso...@gmail.com> wrote:
>
> > Well, I'm definitely an admin on my 32-bit version, although I can't
> > say if the python script is running with elevated privileges or not.
> > I'm using the script I wrote about here:
>
> >http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all...
>
> > It's near the bottom of the article. I tried running cmd.exe as
> > Administrator and then ran my script, but I guess that doesn't change
> > anything since python continued to give me this error.
>
> > I am guessing the UAC is blocking it somehow. Just not sure how to get
> > around that without turning UAC off.
>
> > - Mike
>
> Did you get a chance to try the example against just the current process
> itself? Are you getting access denied errors for every process, ones owned
> by other users, or just having them crop up periodically while looping the
> process list? That would help narrow things down.
>
> Giampaolo has a Windows 7 box which I unfortunately do not, so maybe he will
> have some more helpful tips.
>
> -Jay

Sorry for not responding. I got side-tracked with other projects.
Anyway, all I've tried it with are Admin accounts, both from the
command line and from inside Wing IDE. I think the latter runs a
separate Python process, but I'm not sure how that works.

It doesn't work on Windows 7 Professional 32-bit or Windows 7 Home
Premium 64-bit.

I'll just make a note of this thread on my blog. Thanks for the help.

- Mike
Reply all
Reply to author
Forward
0 new messages