python创建线程示例

yipeiwu_com6年前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端口扫描的实现代码,供大家参考,具体内容如下 获取本机的IP和端口号: import socket def get_my_ip(): t...

python微元法计算函数曲线长度的方法

python微元法计算函数曲线长度的方法

计算曲线长度,根据线积分公式: ,令积分函数 f(x,y,z) 为1,即计算曲线的长度,将其微元化: 其中 根据此时便可在python编程实现,给出4个例子,代码中已有详细注释,不...

合并百度影音的离线数据( with python 2.3)

四种格式的解析: filelist slicelist download.cfg third_party_download.cfg 还是2个文件。替换之前版本即可。 初步测试正常,但时间...

python分析apache访问日志脚本分享

#!/usr/bin/env python # coding=utf-8 #---------------------------------------------------...

学生信息管理系统python版

本文实例为大家分享了python学生信息管理系统的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python # @Time : 2018/3/30 17:37...