Python批量启动多线程代码实例


Posted in Python onFebruary 18, 2020

这篇文章主要介绍了python批量启动多线程代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

建立一个线程池,并将某个线程放入进去

threadpool = []
th = threading.Thread(target=func_name, args=func_args)
threadpool.append(th)

批量加入线程

for i in range(10):
  th = threading.Thread(target=func_name, args=func_args)
  threadpool.append(th)

批量开始线程

for th in threadpool:
    th.start()
for th in threadpool:
  threading.Thread.join(th)

实例如下:

#!/usr/bin/python3.4
# -*- coding: utf-8 -*-

import time
import threading


def matter1(music, test):
  print(test, music)
  # 假设每一首歌曲的时间是2秒
  time.sleep(2)

if __name__ == '__main__':
  # 设定我要听的歌为
  musics = ["music1", "music2", "music3"]
  test = "122678"
  # 开始时间
  start = time.time()

  threadpool = []

  # 传入多个参数
  for music in musics:
    # 传入单个参数请写成
    # args=(arg1,)
    th = threading.Thread(target=matter1, args=(music, test))
    threadpool.append(th)
  for th in threadpool:
    th.start()
  for th in threadpool:
    threading.Thread.join(th)

  # 结束时间
  end = time.time()
  print("完成的时间为:" + str(end - start))

完成同时听三首歌线程,花费时间 2s:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python判断字符串与大小写转换
Jun 08 Python
在Python中marshal对象序列化的相关知识
Jul 01 Python
Python 常用 PEP8 编码规范详解
Jan 22 Python
Python 机器学习库 NumPy入门教程
Apr 19 Python
python3利用venv配置虚拟环境及过程中的小问题小结
Aug 01 Python
基于Python实现人脸自动戴口罩系统
Feb 06 Python
Python 基于FIR实现Hilbert滤波器求信号包络详解
Feb 26 Python
python实现QQ邮箱发送邮件
Mar 06 Python
Python 微信公众号文章爬取的示例代码
Nov 30 Python
Python+Xlwings 删除Excel的行和列
Dec 19 Python
Python基础之hashlib模块详解
May 06 Python
用Python创建简易网站图文教程
Jun 11 Python
基于pytorch padding=SAME的解决方式
Feb 18 #Python
Python中pyecharts安装及安装失败的解决方法
Feb 18 #Python
Python chardet库识别编码原理解析
Feb 18 #Python
解决pytorch-yolov3 train 报错的问题
Feb 18 #Python
利用PyQt中的QThread类实现多线程
Feb 18 #Python
python 对任意数据和曲线进行拟合并求出函数表达式的三种解决方案
Feb 18 #Python
Python识别html主要文本框过程解析
Feb 18 #Python
You might like
PHP中if和or运行效率对比
2014/12/12 PHP
php安装扩展mysqli的实现步骤及报错解决办法
2017/09/23 PHP
WordPress伪静态规则设置代码实例
2020/12/10 PHP
JavaScript起点(严格模式深度了解)
2013/01/28 Javascript
JS将表单导出成EXCEL的实例代码
2013/11/11 Javascript
Jquery模仿Baidu、Google搜索时自动补充搜索结果提示
2013/12/26 Javascript
从js向Action传中文参数出现乱码问题的解决方法
2013/12/29 Javascript
Javascript闭包(Closure)详解
2015/05/05 Javascript
jQuery Ajax全解析
2017/02/13 Javascript
jquery横向纵向鼠标滚轮全屏切换
2017/02/27 Javascript
JS动态插入脚本和插入引用外部链接脚本的方法
2018/05/21 Javascript
详解webpack import()动态加载模块踩坑
2018/07/17 Javascript
webpack-url-loader 解决项目中图片打包路径问题
2019/02/15 Javascript
angular组件间传值测试的方法详解
2020/05/07 Javascript
vue iview实现动态新增和删除
2020/06/17 Javascript
Vue+Java+Base64实现条码解析的示例
2020/09/23 Javascript
详解vue中在父组件点击按钮触发子组件的事件
2020/11/13 Javascript
pip install urllib2不能安装的解决方法
2018/06/12 Python
Python SQL查询并生成json文件操作示例
2018/08/17 Python
Python装饰器基础概念与用法详解
2018/12/22 Python
如何在Django中添加没有微秒的 DateTimeField 属性详解
2019/01/30 Python
查看python安装路径及pip安装的包列表及路径
2019/04/03 Python
python requests包的request()函数中的参数-params和data的区别介绍
2020/05/05 Python
Keras预训练的ImageNet模型实现分类操作
2020/07/07 Python
django使用channels实现通信的示例
2020/10/19 Python
CSS3 实现飘动的云朵动画
2020/12/01 HTML / CSS
教你如何一步一步用Canvas写一个贪吃蛇
2018/10/22 HTML / CSS
介绍一下Ruby的多线程处理
2013/02/01 面试题
社区文艺活动方案
2014/08/19 职场文书
2014年银行员工年终自我评价
2014/09/19 职场文书
营销与策划实训报告
2014/11/05 职场文书
2014年财政所工作总结
2014/11/22 职场文书
社区工作者个人总结
2015/02/28 职场文书
退休教师追悼词
2015/06/23 职场文书
python通过函数名调用函数的几种方法总结
2021/06/07 Python
教你nginx跳转配置的四种方式
2022/07/07 Servers