If I use procname and then run psutil as a shell command I see the change in the name of the process. However, if I use the python psutil module the change doesn't show. (See the code below).
It appears from http://psutil.readthedocs.io/en/latest/#processes that processes are somehow cached. It also says, though, that they are cached at first call, in which case the code below "ought to" work, eh? If the problem is precaching is there a way to tell psutil to refresh its cache?
I know I could capture the output from the os.system call and parse it for the handcrafted process name, but I was hoping I could take advantage of the features of psutil.
import procname
import psutil
import os
procname.setprocname('xyzzzyzyz')
os.system('ps aux | grep xyzzzyzyz')
c = 0
X = psutil.process_iter(attrs=['name'])
for x in X:
c+=1
if x.name() in [ 'xyzzzyzyz', 'python']:
print x
print c
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'name', 'username'])
except psutil.NoSuchProcess:
pass
else:
if pinfo['name'] in ['xyzzzyzyz', 'python']:
print pinfo
psutil author here. No, there currently is no way to refresh Process()
instances. It seems there should be though, as your use case is indeed legitimate. I will add a Process.refresh()
method to invalidate the cache.