线程
import threading
def my_function():
print("子线程开始执行")
# 创建并启动子线程
thread = threading.Thread(target=my_function)
thread.start()
# 打印当前存活的Thread对象数量
print("当前存活的Thread对象数量:", threading.active_count())
进程
from multiprocessing import Process, active_children
def my_function():
print("子进程开始执行")
# 创建并启动子进程
process = Process(target=my_function)
process.start()
# 获取当前存活的进程对象列表
process_list = active_children()
# 打印当前存活的进程对象数量
print("当前存活的进程对象数量:", len(process_list))