python线程调用

线程调用程序如下

import _thread
import time

def run(num):
    print(num)

for i in range(10):
    _thread.start_new_thread(run,(i,)) #入参是调用的函数以及传给函数的参数(参数必须是元组,如果只有一个参数,就如程序所示传参

for i in range(10): #循环10秒 如果没有这个循环程序不会等待线程调用结束
    time.sleep(1)
print('程序结束')

输出如下

1
3
4
56

78

9
2
0
程序结束

进程已结束,退出代码为 0

 

posted @ 2025-05-20 17:18  金一九  阅读(10)  评论(0)    收藏  举报