可以使用 psutil 模块来查看 Python 脚本开启了多少个进程。psutil 是一个跨平台的库,可以查询系统信息,包括进程信息。以下是一个示例代码:

import psutil
import os

# 获取当前进程的pid
pid = os.getpid()

# 获取当前进程对象
p = psutil.Process(pid)

# 获取当前进程的所有子进程
children = p.children(recursive=True)

# 子进程数量
num_children = len(children)

print('当前进程开启了 {} 个子进程'.format(num_children))

在上面的代码中,首先获取当前 Python 脚本的进程 ID(pid),然后使用 psutil 模块获取进程对象(p),再使用 Process 对象的 children() 方法获取所有子进程。最后,使用 len() 函数获取子进程数量,并输出结果。

Python 脚本进程数量查看:使用 psutil 模块

原文地址: https://www.cveoy.top/t/topic/oARp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录