Try psutil:
pip install psutil
Then it is as simple as (using vlc in my case):
for proc in psutil.process_iter(): if proc.name() == 'vlc.exe': print(proc.open_files())
:)
I used psutil (https://github.com/giampaolo/psutil) and wxPython the last time I tried this.
I wrote about it on my blog, but my host is having issues today. Here's a cached version:
Awesome! Can’t wait to check this out – Glances always felt too heavy to me.
How does psutil
work in a container? I would want to Dockerize the daemon, but unclear on how it would access the host’s info (and not the container’s)?
EDIT: just answered my own question: https://github.com/giampaolo/psutil/issues/1011 – long story short, since psutil
mainly works by monitoring data via /proc
, all you have to do is mount the host /proc
into the container.
I think the problem is here:
>call = 'TASKLIST', '/FI', 'imagename eq %s' % process_name
> output = subprocess.check_output(call)
it runs a process, which can create console window (I think)
Try using https://github.com/giampaolo/psutil library for process monitoring
What are the details of your queue. I have a suggestion about how to use a numpy array for a circular buffer that is insanely fast.
As for predicting the size of queue that you can use, psutil can possibly help with that:
>>> import psutil >>> psutil.virtual_memory() svmem(total=10367352832, available=6472179712, percent=37.6, used=8186245120, free=2181107712, active=4748992512, inactive=2758115328, buffers=790724608, cached=3500347392, shared=787554304) >>> psutil.swap_memory() sswap(total=2097147904, used=296128512, free=1801019392, percent=14.1, sin=304193536, sout=677842944)
If you know the size of your code, other variables, and each image, then you can probably calculate the value in question using the info from psutil. I haven't actually done this, though.
You could check if the process id the lock refers to is present. At least on my system. the lockfile seems to be a symlink of the form local_ip:+pid
. On unixy systems you can check this with os.kill(pid, 0)
which should throw a ProcessLookupError if the process wasn't found. Ie something like:
def is_firefox_running(lockfile_path): resolved_path = os.path.realpath(lockfile_path) # Resolve the symlink. ip, pid = os.path.basename(resolved_path).split(":+") try: os.kill(int(pid),0) return True except ProcessLookupError: return False
OTOH, I don't think this will work on Windows, and I'm also assuming that symlink format is consistent across platforms and versions.
Alternatively, and more portably, you could use psutil, and even add further checks there like ensuring it's really the firefox process. Eg:
>>> p = psutil.Process(pid) >>> p.name() 'firefox' >>> p.exe() '/usr/lib/firefox/firefox'
You could also potentially skip the middleman here and just check for a process named firefox without even looking at the lockfile. This could be more brittle though in the event of things like being installed in different locations (if you check the exe()) or other processes with the same name (if you check name()
AFAIK there's not such thing as "logical disks" in the UNIX world. What you probably have in mind is "block device", except that only a subset of block devices seem to be printed by disk_io_counters
according to some funny rules. Probably if you could tweak these rules, you could get the data you want directly from psutil
. Maybe a feature request?
Also, it's not 3kLOC. It can be 5LOC:
stats = {} for device in os.listdir('/sys/block'): if not device.startswith('sd'): continue with open(os.path.join('/sys/block', device, 'stat')) as fh: stats[device] = map(int, fh.read().split())
> whether or not I was missing a shortcut that would be obvious to a more experienced programmer.
I don't think so. This data is already pretty simple to get; shame psutil
doesn't do that already.
To give you an idea, here's how you can get some memory information from my system (Debian):
>>> with open('/proc/meminfo') as f: ... data = {} ... for line in f: ... key, value = line.split(None, 1) ... data[key.strip(':')] = value.strip() ... >>> print("Memory: {MemFree} / {MemTotal} free".format(**data)) Memory: 886436 kB / 3960616 kB free
I don't know, but there's a good chance CentOS will have a completely different system. Windows certainly has a completely different system.
The <code>psutil</code> tries to make a cross platform interface; you could look at that code too for inspiration.
here is a good module to start with: https://github.com/giampaolo/psutil
Once you get that up and running, you can then start to work on tracking things. To reduce load, I would suggest passing scheduling off to the OS (like using cron) and maybe only tracking the top 10 processes.
You will also want to exclude certain processes with a high degree of variability, to prevent it from spamming you.
Having a difficult time installing ‘glances’ can anyone help?
What i have: downloaded (Glances from Github)[https://github.com/nicolargo/glances] python 2.7.14 AND 3.7 installed
what i know:
glances requires python + psutil
however to install psutil it is recommended that i use pip (but pip is already installed if you are using Python 2 >=2.7.9 or Python 3 >=3.4 downloaded from python.org which i am)
"The easiest way to install psutil on Windows is to just use the pre-compiled exe/wheel installers hosted on PyPI via pip:" (link= https://github.com/giampaolo/psutil/blob/master/INSTALL.rst)
C:\Python27\python.exe -m pip install psutil
i copied the above text into both versions 2.7.14 and 3.7 of python and even a cmd window (with "run as administrator" selected) but nothing happens.
the question: what am i doing wrong doing wrong when installing psusil?
There are already network metrics. Take a look at net_io_counters(): http://pythonhosted.org/psutil/#psutil.net_io_counters By running that in a loop you can measure the number of bytes read/written in a second, which is probably what you mean by "network current value". Sample app: https://github.com/giampaolo/psutil/blob/master/scripts/nettop.py