我有一个函数应该使用 psutil 返回给定进程的进程 ID,但由于某种原因它不返回任何内容。
打印所需的变量会显示正确的值。我想我做错了什么。
def pid_find(process_name):
pid = []
for proc in psutil.process_iter():
try:
if proc.name() == process_name:
pid.append(proc.pid)
except psutil.AccessDenied:
pass
try:
process = pid[0]
except IndexError:
raise Exception("Process %s not found" % process_name)
print process # prints the correct value
return process # does not return anything