手机
当前位置:查字典教程网 >脚本专栏 >python >python创建线程示例
python创建线程示例
摘要:复制代码代码如下: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发送邮件接收邮件示例分享

python基础教程之实现石头剪刀布游戏示例

python实现代码行数统计示例分享

使用python装饰器验证配置文件示例

python中mechanize库的简单使用示例

Python 元类使用说明

python使用os模块的os.walk遍历文件夹示例

python解析文件示例

python实现dict版图遍历示例

python获取网页状态码示例

精品推荐
分类导航