简单封装了段Python代码可在liunx下查看指定python脚本运行的线程数量:
- import os
-
- '''
- @:param script_name 脚本名称 示例:run_api.py
- '''
- def get_process_threads_num(script_name):
- process_text = os.popen(f"ps -ef |grep {script_name}").read()
- print(process_text.split("\n")[0])
- process_pid = None
- for text in process_text.split("\n"):
- if f"python {script_name}" in text:
- process_pid = [x for x in text.split(" ") if x][1]
- print("进程pid:", process_pid)
- if process_pid:
- threads_text = os.popen(f'cat /proc/{process_pid}/status |grep Threads').read()
- print("线程数信息:\n", threads_text)
- return threads_text.split("\t")
- else:
- return None
-
-
- print("线程数:", get_process_threads_num("run_api.py"))
-
- '''
- cat /proc/{process_pid}/status |grep Threads
- '''
效果图: