gpt4 book ai didi

c# - 我如何检查进程是否在python中运行

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:16 26 4
gpt4 key购买 nike

在 C# 中,我有:

Process.GetProcessesByName

我在 Python 中寻找类似的东西?

最佳答案

您可以使用 psutil模块:

It currently supports Linux, Windows, OSX, FreeBSD, Sun Solaris both 32-bit and 64-bit with Python versions from 2.4 to 3.3 by using a single code base.

首先安装它:

pip install psutil

然后做类似的事情Process.GetProcessesByName方法做:

Creates an array of new Process components and associates them with all the process resources on the local computer that share the specified process name.

代码:

import psutil

def get_processes_by_name(name):
return [process for process in psutil.process_iter() if process.name == name]


print(get_processes_by_name('python'))

输出:

[<psutil.Process(pid=10217, name='python') at 44007184>, 
<psutil.Process(pid=10223, name='python') at 44007312>
]

关于c# - 我如何检查进程是否在python中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22132126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com