python获取多线程及子线程的返回值
作者:mrr 时间:2022-01-17 20:44:07
最近有个需求,用多线程比较合适,但是我需要每个线程的返回值,这就需要我在threading.Thread的基础上进行封装
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)
def get_result(self):
try:
return self.result # 如果子线程不使用join方法,此处可能会报没有self.result的错误
except Exception:
return None
def foo(a,b,c):
time.sleep(1)
print a*2,b*2,c*2,
return a*2,b*2,c*2
st = time.time()
li = []
for i in xrange(4):
t = MyThread(foo,args=(i,i+1,i+2))
li.append(t)
t.start()
for t in li:
t.join() # 一定要join,不然主线程比子线程跑的快,会拿不到结果
print t.get_result()
et = time.time()
print et - st
执行结果
0 2 4 (0, 2, 4)
4 6 8 2 4 6 (2, 4, 6)
(4, 6, 8)
6 8 10 (6, 8, 10)
1.00200009346
元组中的结果是函数foo的返回值,至于结果为什么这么乱,我猜,是因为各子线程foo的print和主线程print get_result()一起抢占系统资源造成。
下面介绍下python获得子线程的返回值,具体代码如下所示:
import sys
import threading
import Queue
q = Queue.Queue()
def worker1(x, y):
func_name = sys._getframe().f_code.co_name
print "%s run ..." % func_name
q.put((x + y, func_name))
def worker2(x, y):
func_name = sys._getframe().f_code.co_name
print "%s run ...." % func_name
q.put((x - y, func_name))
if __name__ == '__main__':
result = list()
t1 = threading.Thread(target=worker1, name='thread1', args=(10, 5, ))
t2 = threading.Thread(target=worker2, name='thread2', args=(20, 1, ))
print '-' * 50
t1.start()
t2.start()
t1.join()
t2.join()
while not q.empty():
result.append(q.get())
for item in result:
if item[1] == worker1.__name__:
print "%s 's return value is : %s" % (item[1], item[0])
elif item[1] == worker2.__name__:
print "%s 's return value is : %s" % (item[1], item[0])
这是目前最主流的获取线程数据的方法。使用 Queue 库创建队列实例,用来储存和传递线程间的数据。Python 的队列是线程安全的,也就是说多个线程同时访问一个队列也不会有冲突。Python 队列有三种 FIFO 先进先出,FILO 先进后出(类似栈),优先级队列(由单独的优先级参数决定顺序)。使用队列可以实现简单 生产者 – 消费者 模型
总结
以上所述是小编给大家介绍的python获取多线程及子线程的返回值网站的支持!
标签:python,线程,返回值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
oracle 重置序列从指定数字开始的方法详解
2023-07-05 02:40:04
js 混淆加密工具代码
2007-09-22 18:10:00
Linux安装Python3如何和系统自带的Python2并存
2023-08-25 03:42:09
详解MYSQL的备份还原(PHP实现)
2023-10-06 10:44:23
js实现圆形菜单选择器
2023-09-23 19:50:54
![](https://img.aspxhome.com/file/2023/8/56308_0s.jpg)
使用Python更换外网IP的方法
2021-01-25 04:25:46
用isapi rewrite 实现asp的HTML静态化
2007-11-25 12:28:00
![](https://img.aspxhome.com/file/UploadPic/200711/25/20071125124759344s.jpg)
asp,php,.net使用301重定向方法
2007-09-26 14:05:00
python使用正则表达式匹配txt特定字符串(有换行)
2023-03-23 07:46:03
![](https://img.aspxhome.com/file/2023/2/75152_0s.png)
八个有用的WordPress的SQL语句
2009-01-12 18:54:00
asp 存储过程分页代码第1/2页
2011-04-03 10:39:00
![](https://img.aspxhome.com/file/UploadPic/20112/5/201125112228776s.jpg)
使用Python处理json字符串中的非法双引号问题
2021-01-19 19:26:13
ASP实例:读取xml文件的程序
2007-11-04 18:47:00
Python读取文件内容的三种常用方式及效率比较
2023-08-29 23:46:00
![](https://img.aspxhome.com/file/2023/9/67729_0s.png)
nlp自然语言处理学习CBOW模型类实现示例解析
2023-08-08 00:26:03
![](https://img.aspxhome.com/file/2023/8/76178_0s.jpg)
Python+Django+MySQL实现基于Web版的增删改查的示例代码
2023-11-20 02:09:06
![](https://img.aspxhome.com/file/2023/2/64252_0s.png)
python 密码加密与解密的实现
2023-07-31 04:32:38
![](https://img.aspxhome.com/file/2023/3/61433_0s.png)
php中关于普通表单多文件上传的处理方法
2023-11-14 20:21:21
Python报错SyntaxError:unexpected EOF while parsing的解决办法
2023-10-01 10:29:54
form表单的submit方法和submit事件
2008-10-15 11:22:00