我正在尝试创建一个进程监视器,但我无法获得准确的结果,将我的结果与 Windows 任务管理器进行比较。
我一直在使用 psutil,在查看总体 cpu 和内存使用情况时,它似乎工作正常,但对于单个进程来说似乎不太准确。内存使用率总是高于任务管理器,而 CPU 总是随机的。
我在初始化时设置一次进程self.process = psutil.Process(self.pid)
,然后每秒调用一次下面的方法,任务管理器中的进程以恒定的 5.4% cpu 使用率和 130mb ram 运行,但是下面的代码生成:
CPU: 12.5375
Memory 156459008
CPU: 0.0
Memory 156459008
CPU: 0.0
Memory 156459008
CPU: 0.0
Memory 156459008
CPU: 12.5375
Memory 156459008
CPU: 0.0
Memory 156459008
CPU: 0.0
Memory 156459008
Run Code Online (Sandbox Code Playgroud)
示例代码:
def process_info(self):
# I am calling this method twice because I read the first time gets ignored?
ignore_cpu = self.process.cpu_percent(interval=None) / psutil.cpu_count()
time.sleep(0.1)
process_cpu = self.process.cpu_percent(interval=None) / psutil.cpu_count()
# I also …
Run Code Online (Sandbox Code Playgroud) 我一直在尝试安装Peter Chervenski's MultiNEAT
并在运行 setup.py ( python setup.py build_ext
)时遇到问题:
文件“c:/Users/1234/Documents/Alex/multineat/peter-ch-MultiNEAT-f631e2f/setup.py”,第 7 行,在 from site-packages import psutil'
我确保安装了这个模块:使用pip install
了几次,它说:
已经满足要求:psutil in c:\users\1234\appdata\local\programs\python\python36-32\lib\site-packages
我自己检查了这个目录并在psutil
那里找到了模块(我什至删除了它并重新安装了一次)。在那之后,我仍然Python
没有看到相同的错误psutil
。有什么办法可以解决这个问题吗?(我正在使用Windows 10
,最新版本的Python
)
我写了一个包含依赖关系依赖于psutil的包(my-package
依赖于third-party-package
它取决于psutil
).
因为它应该没有任何连接,并没有在服务器上运行gcc
,我与本地准备部署psutil
蟒蛇平台车轮和pip install my-package --download
,然后在服务器上发送的一切.
现在服务器上的所有内容都准备就绪,但出于某种原因,当我运行安装时,pip
拒绝安装psutil
.请注意,服务器是一个red hat 7.2运行pip 7.1.0,virtualenv 1.10.1和python 2.7.5(我无法更改任何版本).
$ pip install /tmp/python_packages/my-package-1.4.zip --no-index
--find-links /tmp/python_packages/ --use-wheel
Ignoring indexes: https://pypi.python.org/simple/
# blablabla, everything goes fine, then
Downloading/unpacking psutil (from third-party-package>=0.9->my-package==1.4)
Could not find any downloads that satisfy the requirement psutil
(from third-party-package>=0.9->my-package==1.4)
Cleaning up...
No distributions at all found for psutil (from third-party-package>=0.9->my-package==1.4)
Storing complete log in /home/anto/.pip/pip.log …
Run Code Online (Sandbox Code Playgroud) 我试图在我的 OS X (12.1) Macbook Pro 上用 python 运行 psutil.net_connections() ,但遇到了 syscall failed 错误。这很奇怪,因为 psutil 的大多数其他功能都工作正常,没有任何问题,但不知何故 net_connections 似乎是唯一不工作的功能。
Python 3.8.9 (default, Oct 26 2021, 07:25:53)
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.net_connections(kind='tcp')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mastermi/Library/Python/3.8/lib/python/site-packages/psutil/__init__.py", line 2161, in net_connections
return _psplatform.net_connections(kind)
File "/Users/mastermi/Library/Python/3.8/lib/python/site-packages/psutil/_psosx.py", line 248, in net_connections
cons = Process(pid).connections(kind)
File "/Users/mastermi/Library/Python/3.8/lib/python/site-packages/psutil/_psosx.py", line 343, in wrapper
return fun(self, …
Run Code Online (Sandbox Code Playgroud) 无论间隔值如何,代码始终返回0.0值.
import psutil
p = psutil.Process()
print p.cpu_percent(interval=1)
print p.cpu_percent(interval=None)
Run Code Online (Sandbox Code Playgroud) 使用以下代码,我可以获得MiB中给定进程的内存消耗:
def memory_usage_psutil():
# return the memory usage in MB
import psutil
process = psutil.Process(os.getpid())
mem = process.get_memory_info()[0] / float(2 ** 20)
return mem
Run Code Online (Sandbox Code Playgroud)
如何更改此值以返回内存消耗百分比?
更新:我需要在终端中为特定进程%MEM
执行top
命令时获取列的当前值.
示例:我需要此函数返回14.2以获取VirtualBox进程的进程ID.
我正在使用PySpark 1.5.2.我UserWarning Please install psutil to have better support with spilling
发出命令后得到了.collect()
为什么会出现此警告?
我该如何安装psutil
?
嘿我正在尝试执行以下命令(使用psutil.Popen和python 2.7):
"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" "C:\docs\?.xlsm"
Run Code Online (Sandbox Code Playgroud)
使用此代码:
dir = u"C:\\docs"
doc = os.listdir(dir)[0]
full_path = os.path.join(dir, doc)
command = u"\"C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.EXE\" \"{}\"".format(full_path)
process = psutil.Popen(command)
Run Code Online (Sandbox Code Playgroud)
但我得到了这个例外:
process = psutil.Popen(command)
File "C:\Python27\lib\site-packages\psutil\__init__.py", line 1370, in __init__
self.__subproc = subprocess.Popen(*args, **kwargs)
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u05ea' in position 102: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我找到了这个相关的问题: subprocess.Popen有一个unicode路径.但是每一个给出的答案都不适用于我.
使用subprocess.Popen(command.encode(locale.getpreferredencoding()))会抛出以下异常:
Traceback (most recent …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种在Windows中以低优先级有效启动多个进程的方法.我试过了 :
def run(command):
# command['Program.exe args1 args2','output_file']
try :
p = subprocess.Popen(command[0] , stdout = command[1])
psutil.Process(p.pid).nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
p.wait()
except Exception as e:
print(e)
raise SystemExit
Run Code Online (Sandbox Code Playgroud)
问题是:低优先级不会立即设置.我开始时会冻结一些.当我仔细观察过程窗口时,我可以看到应用程序的优先级从high_priority开始并切换到low_priority.
我想立即以低优先级启动或找到另一种方法来阻止CPU使用率(现在100%).
然后我在多处理池中使用run命令(每次运行几秒钟).
def safe_run(args):
"""Call run(), catch exceptions."""
try:
run(args)
except Exception as e:
print(args[0])
print(e)
def parallel(commands,nb_proc):
# populate files
# start processes
if len(commands) < 10:
nb_proc = 1
print('Use of {} cpus\n'.format(nb_proc))
pool = mp.Pool(nb_proc)
pool.map(safe_run, commands, chunksize=1)
Run Code Online (Sandbox Code Playgroud)
UPDATE
Test.exe是一个fortran代码:
integer function NumArguments()
integer :: IARGC
NumArguments = IARGC()
end …
Run Code Online (Sandbox Code Playgroud) 当我进入任务管理器时,我可以看到两个部分。应用程序(3) 和后台进程(94)。我想做的就是只获取不在后台的那 3 个。
我的代码:
for proc in psutil.process_iter():
print(proc.name())
Run Code Online (Sandbox Code Playgroud)
但这给了我所有在前台和后台运行的 90 多个应用程序的名称。如何将其中一个与另一个分开?
psutil ×10
python ×10
linux ×1
macos ×1
pip ×1
process ×1
pyspark ×1
python-2.7 ×1
python-2.x ×1
python-wheel ×1
subprocess ×1
windows ×1