Reputation: 21
I would like to know how I can count how many instances of a program are running in the system using python.
For example: if my terminal is open 3 "times", and there are 3 instances of the terminal, how could I count them using python ?
Thanks in advance
Upvotes: 0
Views: 1122
Reputation: 105
The example provided by tenacity works well except if no instances exist matching pname
then it returns None
and the counter would count up every time it was executed so moved it into the function call.
Stackedoverflow has too many pending edits so posting an copy of it instead of editing:
from collections import Counter
from psutil import AccessDenied, process_iter
def get_process_count(pname: str) -> int:
process_ctr = Counter()
for process in process_iter():
try:
process_cmd_list = process.cmdline()
for p in process_cmd_list:
process_ctr[p] += 1
except AccessDenied:
pass
for p, cnt in process_ctr.items():
if pname in p:
return cnt
return 0
Upvotes: 0
Reputation: 582
This problem is fairly very straightforward if one uses the psutil
package in python3
Consider the code snippet below.
import psutil
from collections import Counter
process_ctr = Counter()
for process in psutil.process_iter():
try:
process_cmd_list = process.cmdline()
for p in process_cmd_list:
process_ctr[p] += 1
except Exception:
continue
for pname, cnt in process_ctr.items():
print(pname, cnt)
Upon running this code, one will get the process name and count printed to stdout
Testing: I opened 3 terminals and I noticed this in the O/P: -bash 3
Sample O/P:
--pdf-renderer 2
--js-flags=--jitless 2
--renderer-client-id=657 1
--launch-time-ticks=75930041264 1
--renderer-client-id=658 1
--launch-time-ticks=76065123981 1
--seatbelt-client=151 4
--renderer-client-id=659 1
--launch-time-ticks=76065777110 1
--renderer-client-id=663 1
--launch-time-ticks=76077686392 1
--renderer-client-id=100 1
--launch-time-ticks=1656826210 1
--renderer-client-id=665 1
--launch-time-ticks=76250675532 1
--seatbelt-client=168 11
--renderer-client-id=666 1
--launch-time-ticks=76252057630 1
-bash 3
/System/Library/PrivateFrameworks/CoreFollowUp.framework/Versions/A/Support/followupd 1
/System/Library/PrivateFrameworks/PhotoAnalysis.framework/Versions/A/Support/photoanalysisd 1
/System/Library/CoreServices/ScopedBookmarkAgent 1
--renderer-client-id=672 1
--launch-time-ticks=76542560240 1
--renderer-client-id=676 1
--launch-time-ticks=76557205901 1
Note:
try
, catch
is needed as psutil
cannot access some process details, as they are owned by the kernel.Based on user feedback, adding some neat code, just to get the count for the process of interest.
import psutil
from collections import Counter
process_ctr = Counter()
def get_process_count(pname):
for process in psutil.process_iter():
try:
process_cmd_list = process.cmdline()
for p in process_cmd_list:
process_ctr[p] += 1
except Exception:
continue
for p, cnt in process_ctr.items():
if pname in p:
return cnt
if __name__ == '__main__':
cnt = get_process_count('qterminal')
print(f"The cnt is:{cnt}")
Upvotes: 4