我是靠谱客的博主 和谐牛排,这篇文章主要介绍python创建线程示例,现在分享给大家,希望可以做个参考。

复制代码 代码如下:

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创建线程示例的全部内容,更多相关python创建线程示例内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(211)

评论列表共有 0 条评论

立即
投稿
返回
顶部