神剑山庄资源网 Design By www.hcban.com

复制代码 代码如下:
import threading
from time import sleep

def test_func(id):
    for i in range(0,5):
        sleep(1)
        print('thread %d is running %d' % (id,i))

threads = []
for i in range(0,3):
    t = threading.Thread(target=test_func, args=(i,))
    threads.append(t)

for t in threads:
    t.start()

for t in threads:
    t.join()

从输出结果可以看到,3个线程是交替的执行的

 

标签:
python线程

神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com

评论“python创建线程示例”

暂无python创建线程示例的评论...