python创建线程示例_python教程-查字典教程网
python创建线程示例
python创建线程示例
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:importthreadingfromtimeimportsleepdeftest_func(id):foriinrang...

复制代码 代码如下:

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学习
    脚本专栏子分类