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抓取网页中图片并保存到本地
Dec 01 Python
python 执行文件时额外参数获取的实例
Dec 18 Python
python 获取图片分辨率的方法
Jan 08 Python
python实现nao机器人身体躯干和腿部动作操作
Apr 29 Python
python3.6+selenium实现操作Frame中的页面元素
Jul 16 Python
wxpython布局的实现方法
Nov 01 Python
python中使用paramiko模块并实现远程连接服务器执行上传下载功能
Feb 29 Python
python matplotlib imshow热图坐标替换/映射实例
Mar 14 Python
解决Python在导入文件时的FileNotFoundError问题
Apr 10 Python
在keras中model.fit_generator()和model.fit()的区别说明
Jun 17 Python
python 使用多线程创建一个Buffer缓存器的实现思路
Jul 02 Python
了解一下python内建模块collections
Sep 07 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
Terran兵种对照表
2020/03/14 星际争霸
PHP分页显示制作详细讲解
2006/10/09 PHP
Warning: session_destroy() : Trying to destroy uninitialized sessionq错误
2011/06/16 PHP
PHP不使用递归的无限级分类简单实例
2016/11/05 PHP
php中上传文件的的解决方案
2018/09/25 PHP
JQuery 学习笔记 选择器之五
2009/07/23 Javascript
javascript instanceof 内部机制探析
2010/10/15 Javascript
jQuery 1.9使用$.support替代$.browser的使用方法
2014/05/27 Javascript
一不小心就做错的JS闭包面试题
2015/11/25 Javascript
基于javascript实现全国省市二级联动下拉选择菜单
2016/01/28 Javascript
第六篇Bootstrap表格样式介绍
2016/06/21 Javascript
EditPlus中的正则表达式 实战(2)
2016/12/15 Javascript
基于jPlayer三分屏的制作方法
2016/12/21 Javascript
Mongoose实现虚拟字段查询的方法详解
2017/08/15 Javascript
JavaScript时间戳与时间日期间相互转换
2017/12/11 Javascript
JS遍历DOM文档树的方法实例详解
2018/04/03 Javascript
解决layui前端框架 form表单,table表等内置控件不显示的问题
2018/08/19 Javascript
js canvas实现写字动画效果
2018/11/30 Javascript
小程序的上传文件接口的注意要点解析
2019/09/17 Javascript
vue通过过滤器实现数据格式化
2020/07/20 Javascript
微信小程序开发数据缓存基础知识辨析及运用实例详解
2020/11/06 Javascript
[10:54]Team Spirit vs Navi
2018/06/07 DOTA
Python中字典(dict)和列表(list)的排序方法实例
2014/06/16 Python
python3+PyQt5实现支持多线程的页面索引器应用程序
2018/04/20 Python
对numpy Array [: ,] 的取值方法详解
2018/07/02 Python
对python 判断数字是否小于0的方法详解
2019/01/26 Python
简单了解python gevent 协程使用及作用
2019/07/22 Python
PyCharm 2019.3发布增加了新功能一览
2019/12/08 Python
Django执行源生mysql语句实现过程解析
2020/11/12 Python
CSS3 3D立方体效果示例-transform也不过如此
2016/12/05 HTML / CSS
深入CSS3 动画效果的总结详解
2013/05/09 HTML / CSS
GEOX鞋美国官方网站:意大利会呼吸的鞋
2017/07/12 全球购物
意大利网上书店:LaFeltrinelli
2020/06/12 全球购物
致毕业季:你如何做好自己的职业生涯规划书?
2019/07/01 职场文书
Python趣味实战之手把手教你实现举牌小人生成器
2021/06/07 Python
Python查找算法的实现 (线性、二分,分块、插值查找算法)
2022/04/24 Python