龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > python编程 >

python创建线程示例

时间:2014-05-12 15:39来源:网络整理 作者:网络 点击:
分享到:
这篇文章主要介绍了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个线程是交替的执行的

 

精彩图集

赞助商链接