python 2种创建多线程的方法
多个线程是可以操作同一个全局变量的,因此,可以通过这种方式来判断所有线程的执行进度
# 第一种方法:将要执行的方法作为参数传给Thread的构造方法
import threading
import time liste = ["a","s","v","dd","ee"] def action(a):
print threading.currentThread()
time.sleep(2)
liste.remove(a)
print liste
print("-------------") if __name__ == "__main__":
for item in liste:
a = threading.Thread(target = action, args = (item,))
a.start()
# 第二种方法:从Thread继承,并重写run()
#!-*- coding:utf-8 -*-
import threading
import time liste = ["a","s","v","dd","ee"] class myThread(threading.Thread):
def __init__(self, arg):
# threading.Thread.__init__(self) # 这2种方法都可以使用
super(myThread, self).__init__()
self.arg = arg def run(self):# 这个函数定义每个线程要执行的代码,可以嵌套函数
self.action() def action(self):
print threading.currentThread()
time.sleep(2)
liste.remove(self.arg)
print(liste)
print("-------------") if __name__ == "__main__":
for item in liste:
a = myThread(item)
a.start()
多线程执行代码块,并获取每个线程中代码块返回的数据
import threading
class MyThread(threading.Thread): def __init__(self,func,args=()):
super(MyThread,self).__init__()
self.func = func
self.args = args def run(self):
self.result = self.func(self.args)
print(self.result) def get_result(self):
try:
return self.result # 如果子线程不使用join方法,此处可能会报没有self.result的错误
except Exception:
return None def MySSH(dic):
return 0 if __name__=="__main__":
result_list = []
thread_list = []
for item in host_list:
t = MyThread(MySSH, args=(item))
thread_list.append(t)
t.start() for t in thread_list:
t.join() # 一定要join,不然主线程比子线程跑的快,会拿不到结果
result_list.append(t.get_result())
return result_list
python 2种创建多线程的方法的更多相关文章
- 【Java】学习路径46-两种创建多线程的方法、以及在匿名内部类创建线程
两种方法: 1.创建一个继承自Thread的线程类,然后再main(不限)中构造这个线程类对象.方法在之前讲过. 2.创建一个使用Runnable接口的线程类,然后在main(不限)中构造这个Runn ...
- Python几种创建list的方法的效率对比
我们用 生成一个0到((1万倍n)-1)的list做例子 首先这种方式复杂度为平方级 ''' def test1(n): lst = [] for i in range(n*10000): lst = ...
- python 读写、创建 文件的方法(必看)
python 读写.创建 文件的方法(必看) 更新时间:2016年09月12日 10:26:41 投稿:jingxian 我要评论下面小编就为大家带来一篇python 读写.创建 文件的方法(必看). ...
- 【Java 线程的深入研究1】Java 提供了三种创建线程的方法
Java 提供了三种创建线程的方法: 通过实现 Runnable 接口: 通过继承 Thread 类本身: 通过 Callable 和 Future 创建线程. 1.通过实现 Runnable 接口来 ...
- C#两种创建快捷方式的方法
C#两种创建快捷方式的方法http://www.cnblogs.com/linmilove/archive/2009/06/10/1500989.html
- 【转】python 三种遍历list的方法
[转]python 三种遍历list的方法 #!/usr/bin/env python # -*- coding: utf-8 -*- if __name__ == '__main__': list ...
- Python | 面试必问,线程与进程的区别,Python中如何创建多线程?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Python专题第20篇文章,我们来聊聊Python当中的多线程. 其实关于元类还有很多种用法,比如说如何在元类当中设置参数啦,以及一 ...
- python 用Threading创建多线程
#-*-coding:utf-8-*- '''python标准库提供了两个多线程模块,分别为thread和threading, 其中thread模块是低级模块,threading是高级模块,对thre ...
- 09 Python两种创建类的方式
第一种比较普遍的方式: class Work(): def __init__(self,name): self.name = name w = Work('well woker') 这样就简单创建了一 ...
随机推荐
- A%G^C006
AGC006 快乐翻题解\(\sqrt{}\) A Prefix and Suffix 这一场我怎么一道都不会啊/kk https://agc006.contest.atcoder.jp/submis ...
- Spring Transaction 使用入门
一.开篇陈述 1.1 写文缘由 最近在系统学习spring框架IoC.AOP.Transaction相关的知识点,准备写三篇随笔记录学习过程中的感悟.这是第一篇,记录spring Transactio ...
- SpringBoot整合MyBatis例子
1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- mac系统InetAddress.getLocalHost().getHostAddress() 很慢
java.net.InetAddress.getLocalHost() 这个调用在新版的mac os中可能会耗时很久. 解决办法有两种: 1. 就是把本机的hostname 添加到 hosts 中,例 ...
- An overview of time series forecasting models
An overview of time series forecasting models 2019-10-04 09:47:05 This blog is from: https://towards ...
- SpatiaLite, load spatial extension first
- 图片上传: ajax-formdata-upload
传送门:https://www.cnblogs.com/qiumingcheng/p/6854933.html ajax-formdata-upload.html <!DOCTYPE html& ...
- EChart 标题 title 样式,x轴、y轴坐标显示,调整图表位置等
示例里工作一般情况是够用了,更复杂的可以查询教程: title 官方解说:http://echarts.baidu.com/option.html#title 坐标相关: X轴:http://echa ...
- MongoTemplate进行增删改查
转载自:https://blog.csdn.net/sinat_35821285/article/details/83511203#commentBox 1.首先pom.xml <!-- spr ...
- Android Studio Error:Execution failed for task ':app:compileDebugJavaWithJavac' 根本解决方法
造成这种异常的原因有很多.具体的还是要去终端编译,查看到底是什么地方出错了,然后具体问题具体分析. 终端进入项目的根目录,然后输入命令 gradlew compileDebugJavaWithJava ...