Why is psutil.process_iter() running so slowly?
Asked Answered
B

1

7

When I run the below code, it takes over two minutes to cycle through all the processes. Is this normal? I have been using psutil.process_iter() for a while, and I don't remember it taking this long before.

import psutil
import datetime as dt       

for proc in psutil.process_iter():
    try:
        pinfo = proc.as_dict(attrs=['pid', 'name'])
    except psutil.NoSuchProcess:
        pass
    else:
        print(pinfo)
        print dt.datetime.now()

Output:

{'pid': 0, 'name': 'System Idle Process'}
2017-07-11 01:55:45.392000
{'pid': 4, 'name': 'System'}
2017-07-11 01:55:45.394000
{'pid': 256, 'name': 'acrotray.exe'}
2017-07-11 01:55:45.397000
{'pid': 404, 'name': None}
2017-07-11 01:55:46.670000
{'pid': 568, 'name': None}
2017-07-11 01:55:47.750000
{'pid': 604, 'name': None}
2017-07-11 01:55:48.996000
{'pid': 608, 'name': None}
2017-07-11 01:55:50.293000
{'pid': 700, 'name': None}
2017-07-11 01:55:51.492000
{'pid': 708, 'name': None}
2017-07-11 01:55:52.587000
{'pid': 784, 'name': None}
2017-07-11 01:55:55.319000
{'pid': 836, 'name': None}
2017-07-11 01:55:56.537000
{'pid': 844, 'name': None}
2017-07-11 01:55:57.898000
{'pid': 948, 'name': None}
2017-07-11 01:55:59.103000
{'pid': 1012, 'name': None}
2017-07-11 01:56:00.267000
{'pid': 1036, 'name': None}
2017-07-11 01:56:02.072000
{'pid': 1112, 'name': None}
2017-07-11 01:56:04.072000
{'pid': 1120, 'name': None}
2017-07-11 01:56:05.266000
{'pid': 1176, 'name': None}
2017-07-11 01:56:06.476000
{'pid': 1292, 'name': None}
2017-07-11 01:56:08.451000
{'pid': 1352, 'name': None}
2017-07-11 01:56:09.660000
{'pid': 1432, 'name': None}
2017-07-11 01:56:10.915000
{'pid': 1456, 'name': None}
2017-07-11 01:56:12.206000
{'pid': 1480, 'name': 'svchost.exe'}
2017-07-11 01:56:12.208000
{'pid': 1516, 'name': None}
2017-07-11 01:56:13.293000
{'pid': 1576, 'name': None}
2017-07-11 01:56:14.525000
{'pid': 1640, 'name': None}
2017-07-11 01:56:15.831000
{'pid': 1660, 'name': None}
2017-07-11 01:56:17.055000
{'pid': 1728, 'name': None}
2017-07-11 01:56:19.045000
{'pid': 1992, 'name': 'RAVBg64.exe'}
2017-07-11 01:56:19.047000
{'pid': 2084, 'name': None}
2017-07-11 01:56:20.311000
{'pid': 2140, 'name': None}
2017-07-11 01:56:21.391000
{'pid': 2192, 'name': 'googledrivesync.exe'}
2017-07-11 01:56:21.393000
{'pid': 2220, 'name': None}
2017-07-11 01:56:22.756000
{'pid': 2340, 'name': None}
2017-07-11 01:56:23.984000
{'pid': 2552, 'name': None}
2017-07-11 01:56:25.180000
{'pid': 2612, 'name': None}
2017-07-11 01:56:27.439000
{'pid': 2660, 'name': None}
2017-07-11 01:56:29.048000
{'pid': 2676, 'name': None}
2017-07-11 01:56:30.170000
{'pid': 2700, 'name': None}
2017-07-11 01:56:31.481000
{'pid': 2720, 'name': None}
2017-07-11 01:56:32.927000
{'pid': 2760, 'name': None}
2017-07-11 01:56:34.823000
{'pid': 2844, 'name': None}
2017-07-11 01:56:37.056000
{'pid': 2872, 'name': None}
2017-07-11 01:56:38.139000
{'pid': 2880, 'name': None}
2017-07-11 01:56:39.580000
{'pid': 2912, 'name': None}
2017-07-11 01:56:40.814000
{'pid': 2976, 'name': 'taskhostw.exe'}
2017-07-11 01:56:40.815000
{'pid': 3408, 'name': 'sihost.exe'}
2017-07-11 01:56:40.817000
{'pid': 3436, 'name': None}
2017-07-11 01:56:42.011000
{'pid': 3788, 'name': None}
2017-07-11 01:56:44.004000
{'pid': 3824, 'name': None}
2017-07-11 01:56:45.243000
{'pid': 4160, 'name': None}
2017-07-11 01:56:46.458000
{'pid': 4308, 'name': None}
2017-07-11 01:56:49.129000
{'pid': 4536, 'name': None}
2017-07-11 01:56:50.274000
{'pid': 4788, 'name': 'dllhost.exe'}
2017-07-11 01:56:50.276000
{'pid': 5136, 'name': None}
2017-07-11 01:56:51.445000
{'pid': 5264, 'name': None}
2017-07-11 01:56:52.771000
{'pid': 5284, 'name': None}
2017-07-11 01:56:54.653000
{'pid': 5408, 'name': None}
2017-07-11 01:56:56.155000
{'pid': 5492, 'name': None}
2017-07-11 01:56:57.365000
{'pid': 5508, 'name': 'AnyDesk.exe'}
2017-07-11 01:56:57.366000
{'pid': 5660, 'name': None}
2017-07-11 01:56:58.456000
{'pid': 5764, 'name': 'svchost.exe'}
2017-07-11 01:57:00.759000
{'pid': 5936, 'name': 'RuntimeBroker.exe'}
2017-07-11 01:57:00.761000
{'pid': 5948, 'name': None}
2017-07-11 01:57:02.096000
{'pid': 5964, 'name': 'explorer.exe'}
2017-07-11 01:57:02.098000
{'pid': 6124, 'name': None}
2017-07-11 01:57:04.011000
{'pid': 6160, 'name': 'googledrivesync.exe'}
2017-07-11 01:57:04.013000
{'pid': 6348, 'name': None}
2017-07-11 01:57:05.390000
{'pid': 6400, 'name': None}
2017-07-11 01:57:06.715000
{'pid': 6476, 'name': None}
2017-07-11 01:57:07.927000
{'pid': 6544, 'name': None}
2017-07-11 01:57:09.148000
{'pid': 6556, 'name': 'jusched.exe'}
2017-07-11 01:57:09.150000
{'pid': 6600, 'name': None}
2017-07-11 01:57:10.441000
{'pid': 6856, 'name': None}
2017-07-11 01:57:11.516000
{'pid': 6916, 'name': None}
2017-07-11 01:57:12.739000
{'pid': 7040, 'name': 'AnyDesk.exe'}
2017-07-11 01:57:14.822000
{'pid': 7052, 'name': None}
2017-07-11 01:57:16.707000
{'pid': 7112, 'name': None}
2017-07-11 01:57:18.072000
{'pid': 7328, 'name': 'HD-Agent.exe'}
2017-07-11 01:57:18.074000
{'pid': 7860, 'name': 'ShellExperienceHost.exe'}
2017-07-11 01:57:18.076000
{'pid': 8048, 'name': 'SearchUI.exe'}
2017-07-11 01:57:18.078000
{'pid': 8240, 'name': None}
2017-07-11 01:57:19.279000
{'pid': 8444, 'name': 'RtkNGUI64.exe'}
2017-07-11 01:57:19.281000
{'pid': 8492, 'name': 'python.exe'}
2017-07-11 01:57:19.282000
{'pid': 8628, 'name': 'MSASCuiL.exe'}
2017-07-11 01:57:19.284000
{'pid': 9000, 'name': None}
2017-07-11 01:57:20.370000
{'pid': 9012, 'name': 'nvtray.exe'}
2017-07-11 01:57:20.372000
{'pid': 9132, 'name': None}
2017-07-11 01:57:21.634000
{'pid': 9188, 'name': 'NvBackend.exe'}
2017-07-11 01:57:21.636000
{'pid': 11876, 'name': 'mmc.exe'}
2017-07-11 01:57:21.638000
{'pid': 340664, 'name': None}
2017-07-11 01:57:24.284000
{'pid': 1341072, 'name': 'OneDrive.exe'}
2017-07-11 01:57:24.285000
{'pid': 2065140, 'name': 'taskhostw.exe'}
2017-07-11 01:57:24.287000
{'pid': 2091584, 'name': None}
2017-07-11 01:57:25.505000
{'pid': 2676980, 'name': None}
2017-07-11 01:57:26.867000
{'pid': 2684020, 'name': None}
2017-07-11 01:57:28.049000
{'pid': 2684616, 'name': None}
2017-07-11 01:57:29.613000
{'pid': 2684628, 'name': None}
2017-07-11 01:57:31.347000
{'pid': 3016072, 'name': 'SkypeHost.exe'}
2017-07-11 01:57:31.348000
{'pid': 4308396, 'name': 'splwow64.exe'}
2017-07-11 01:57:31.350000
{'pid': 4510700, 'name': None}
2017-07-11 01:57:32.848000
{'pid': 4925868, 'name': None}
2017-07-11 01:57:34.734000
{'pid': 4964188, 'name': 'TvUpdateInfo.exe'}
2017-07-11 01:57:34.736000
{'pid': 4966736, 'name': None}
2017-07-11 01:57:36.912000
{'pid': 4967852, 'name': None}
2017-07-11 01:57:37.995000
{'pid': 5006116, 'name': 'TeamViewer.exe'}
2017-07-11 01:57:37.997000
{'pid': 5016052, 'name': None}
2017-07-11 01:57:39.229000
{'pid': 5021384, 'name': None}
2017-07-11 01:57:40.531000
{'pid': 5021508, 'name': None}
2017-07-11 01:57:41.750000
{'pid': 5022036, 'name': None}
2017-07-11 01:57:43.765000
{'pid': 5022176, 'name': None}
2017-07-11 01:57:45.023000
{'pid': 5258664, 'name': 'chrome.exe'}
2017-07-11 01:57:45.024000
{'pid': 5469448, 'name': 'chrome.exe'}
2017-07-11 01:57:45.026000
{'pid': 5479292, 'name': 'chrome.exe'}
2017-07-11 01:57:45.028000
{'pid': 5500760, 'name': 'chrome.exe'}
2017-07-11 01:57:45.029000
{'pid': 5539972, 'name': 'chrome.exe'}
2017-07-11 01:57:45.031000
{'pid': 5540664, 'name': 'chrome.exe'}
2017-07-11 01:57:45.035000
{'pid': 5546088, 'name': 'chrome.exe'}
2017-07-11 01:57:45.044000
{'pid': 5561092, 'name': 'chrome.exe'}
2017-07-11 01:57:45.048000
{'pid': 5561948, 'name': 'chrome.exe'}
2017-07-11 01:57:45.049000
{'pid': 5562356, 'name': 'chrome.exe'}
2017-07-11 01:57:45.055000
{'pid': 6958780, 'name': 'bxlaui.exe'}
2017-07-11 01:57:45.065000
{'pid': 7234036, 'name': 'bplus.exe'}
2017-07-11 01:57:45.071000
{'pid': 7261260, 'name': 'InstallAgent.exe'}
2017-07-11 01:57:45.080000
{'pid': 7261348, 'name': 'InstallAgentUserBroker.exe'}
2017-07-11 01:57:45.085000
{'pid': 7488312, 'name': 'bplus.exe'}
2017-07-11 01:57:45.089000
{'pid': 7755192, 'name': 'WINTRV.EXE'}
2017-07-11 01:57:45.100000
{'pid': 7758992, 'name': 'blpaddinhost.exe'}
2017-07-11 01:57:45.106000
{'pid': 7767328, 'name': 'chrome.exe'}
2017-07-11 01:57:45.115000
{'pid': 7790252, 'name': 'blpcbbap.exe'}
2017-07-11 01:57:45.119000
{'pid': 7801644, 'name': 'bxlartd.exe'}
2017-07-11 01:57:45.121000
{'pid': 7804804, 'name': 'bplus.exe'}
2017-07-11 01:57:45.125000
{'pid': 7804824, 'name': None}
2017-07-11 01:57:46.232000
{'pid': 7806284, 'name': 'bbcomm.exe'}
2017-07-11 01:57:46.233000
{'pid': 7806924, 'name': 'wui2manager.exe'}
2017-07-11 01:57:46.235000
{'pid': 7821280, 'name': None}
2017-07-11 01:57:49.104000
{'pid': 7844804, 'name': 'python.exe'}
2017-07-11 01:57:49.105000
{'pid': 7851064, 'name': 'python.exe'}
2017-07-11 01:57:49.107000
{'pid': 7854772, 'name': 'blpsmarthost.exe'}
2017-07-11 01:57:49.109000
{'pid': 7854856, 'name': 'ipython.exe'}
2017-07-11 01:57:49.111000
{'pid': 7855228, 'name': None}
2017-07-11 01:57:50.187000
{'pid': 7857920, 'name': 'cmd.exe'}
2017-07-11 01:57:50.189000
{'pid': 7858064, 'name': 'conhost.exe'}
2017-07-11 01:57:50.191000
{'pid': 7858352, 'name': 'bplus.exe'}
2017-07-11 01:57:50.193000
{'pid': 7858368, 'name': 'bplus.exe'}
2017-07-11 01:57:50.194000
{'pid': 7858372, 'name': None}
2017-07-11 01:57:51.523000
{'pid': 7872880, 'name': 'conhost.exe'}
2017-07-11 01:57:51.526000
{'pid': 7874176, 'name': 'ipython.exe'}
2017-07-11 01:57:51.534000
{'pid': 7874248, 'name': 'cmd.exe'}
2017-07-11 01:57:51.538000
{'pid': 7874272, 'name': 'conhost.exe'}
2017-07-11 01:57:51.540000
{'pid': 7874328, 'name': 'python.exe'}
2017-07-11 01:57:51.544000
{'pid': 7876140, 'name': 'chrome.exe'}
2017-07-11 01:57:51.554000
{'pid': 7879120, 'name': 'smartscreen.exe'}
2017-07-11 01:57:51.560000
{'pid': 7879584, 'name': 'cmd.exe'}
2017-07-11 01:57:51.569000
{'pid': 7879592, 'name': 'conhost.exe'}
2017-07-11 01:57:51.573000
Batton answered 11/7, 2017 at 6:2 Comment(6)
Are you sure psutil is a bottleneck and not printing?Welltimed
If you look at the differences between the timestamps, it are mainly the programs with name: None that take a lot of timeShulman
Yes, it's not the printing. I ran this code on my laptop and it completes in less than 2 seconds. It's something about this particular machine. Otherwise, I have no speed issues with it, it's not a slow computer otherwise.Batton
I just restarted the Windows 10 machine I was having this issue on, and now the code completes in less than 1 second. Go figure!Batton
Not that this is particularly helpful, but I'm running into the exact same issue on a VERY fast machine, win7. Any resolution?Conner
I've also run into this issue. Windows 10.Precedential
P
0

There may be thousands of exe files run on server desktop where other employees also working… then it will take time. But there is a solution for that. Please keep you exe name starting with AAA+desired_name.exe, then the iteration find the exe at the beginning

Prefect answered 22/10, 2022 at 2:43 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Osugi
In fact this is not true. Documentation states that the processes are given in order of ascending PID, not ascending process name, cf. psutil.readthedocs.io/en/latest: "Sorting order in which processes are returned is based on their PID."Gilgamesh

© 2022 - 2024 — McMap. All rights reserved.