I’m struggling to understand this block of code:
import psutil
import time
def main():
process_dict = {}
psutil.process_iter.cache_clear()
processes = psutil.process_iter()
for process in processes:
duration = time.time() - process.create_time()
if process.name() not in process_dict.keys(): # Find the longest running process for each application
process_dict[process.name()] = duration
elif process.name() in process_dict.keys() and duration > process_dict[process.name()]:
process_dict[process.name()] = duration
print(f'{process.name()} : {process.create_time()}')
The output seems to be only partially correct:
System Idle Process : 0.0
System : 0.0
: 1722366605.6514304
Registry : 1722366605.7470324
WidgetService.exe : 1722646907.9835372
MoUsoCoreWorker.exe : 1722366742.625491
smss.exe : 1722366607.7051022
csrss.exe : 1722366616.5462563
svchost.exe : 1722366619.7795236
...
- Why is the
create_time()
for the first 2 processes 0.0? That would mean they’ve been running for 54 years which clearly isn’t correct. - Why is there an empty string for the third process? What is this mysterious process with no name?
- I want to find how long each application on my computer has been running, yet when I convert
duration
into hours and minutes for some processes, they’re quite lengthy (e.g. 82 hours) which can’t be correct as I shut down my laptop at the end of each day so all processes should be less than 24 hours. Why does this happen?