-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelapsed_time.pyw
More file actions
91 lines (73 loc) · 3.5 KB
/
elapsed_time.pyw
File metadata and controls
91 lines (73 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import psutil
import shelve
import time
import os
def get_elapsed_time_from_shelve(shelve_file):
# Create the "Shelve files" directory if it doesn't exist
directory = "Shelve files"
if not os.path.exists(directory):
os.makedirs(directory)
# Join the directory and shelve file name to get the full path
shelve_path = os.path.join(directory, os.path.basename(shelve_file))
# Create or open the shelve file
with shelve.open(shelve_path) as shelf:
# Return the value associated with 'start_time', defaulting to 0.0 if the key doesn't exist
return float(shelf.get('start_time', 0.0))
def format_elapsed_time(elapsed_time):
# Convert elapsed time to hours, minutes, and seconds
hours, remainder = divmod(elapsed_time, 3600)
minutes, seconds = divmod(remainder, 60)
return hours, minutes, seconds
def save_elapsed_time_to_shelve(shelve_file, elapsed_time):
# If the shelve file exists, read the previously recorded elapsed time
existing_elapsed_time = get_elapsed_time_from_shelve(shelve_file)
# Add the new elapsed time to the existing value
total_elapsed_time = existing_elapsed_time + elapsed_time
# Join the directory and shelve file name to get the full path
directory = "Shelve files"
shelve_path = os.path.join(directory, os.path.basename(shelve_file))
# Save the total elapsed time to the shelve file
with shelve.open(shelve_path) as shelf:
shelf['start_time'] = total_elapsed_time
# Convert the total elapsed time to a human-readable form
hours, minutes, seconds = format_elapsed_time(total_elapsed_time)
readable_file_name = f"{shelve_file}_readable.txt"
# Create the readable file if it doesn't exist
if not os.path.exists(readable_file_name):
with open(readable_file_name, 'w') as file:
file.write('0 hours, 0 minutes, 0 seconds')
# Save total elapsed time to the readable file
with open(readable_file_name, 'w') as file:
file.write(f"{int(hours)} hours, {int(minutes)} minutes, {int(seconds)} seconds")
def monitor_process(process_name, shelve_file):
counter_started = False
start_time = None
while True:
process_found = False
# Check for the monitored process
for process in psutil.process_iter(['pid', 'name']):
if process.info['name'] == process_name:
process_found = True
if not counter_started:
counter_started = True
start_time = time.time()
break
# Continuously update elapsed time at every iteration of while loop whilst monitored process is running.
end_time = time.time()
elapsed_time = end_time - start_time
start_time = time.time()
save_elapsed_time_to_shelve(shelve_file, elapsed_time)
break
# If the monitored process is not found, stop the counter
if not process_found and counter_started:
counter_started = False
end_time = time.time()
elapsed_time = end_time - start_time
save_elapsed_time_to_shelve(shelve_file, elapsed_time)
# Add a small delay to avoid excessive CPU usage
time.sleep(5)
if __name__ == "__main__":
# Replace "DCS.exe" with the name of the process you want to monitor
process_to_monitor = "DCS.exe"
shelve_file = "elapsed_time"
monitor_process(process_to_monitor, shelve_file)