python多线程id获取

demo

import threading
import time

def print_thread_info(thread_name):
    """线程函数,打印线程名称和ID以及一些文本"""
    for i in range(3):
        time.sleep(1)
        thread_id = threading.current_thread().ident
        print(f"{thread_name} (ID: {thread_id}): 这是第 {i+1} 次打印")

# 创建线程列表
threads = []

# 创建并启动线程
for i in range(5):
    thread = threading.Thread(target=print_thread_info, args=(f"线程{i+1}",))
    thread.start()
    threads.append(thread)

# 等待所有线程完成
for thread in threads:
    thread.join()

print("所有线程已完成.")

output

posted @ 2024-01-24 10:00  __username  阅读(71)  评论(0编辑  收藏  举报

本文作者:DIVMonster

本文链接:https://www.cnblogs.com/guangzan/p/12886111.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。