python多线程获取子线程任务返回值
今天想实现多线程更新资产信息,所以使用到了threading,但是我需要每个线程的返回值,这就需要我在threading.Thread的基础上进行封装
def auto_asset(node):
ret = salt.remote_grains_execution_sigle(node)
asset_info={}
asset_info['os']= ret[node]['oscodename']
asset_info['kernelrelease']= ret[node]['kernelrelease']
asset_info['cpu_model']= ret[node]['cpu_model']
asset_info['dns']= ','.join(ret[node]['dns']['ip4_nameservers'])
asset_info['serialnumber'] = ret[node]['serialnumber']
asset_info['virtual'] = ret[node]['virtual']
asset_info['localhost'] = ret[node]['localhost']
asset_info['mem_total'] = ret[node]['mem_total']
asset_info['num_cpus'] = ret[node]['num_cpus']
asset_info['ip4_interfaces'] = " ".join(ret[node]['ip4_interfaces']['eth0'])
asset_info['hwaddr_interfaces'] = ret[node]['hwaddr_interfaces']['eth0']
return asset_info
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
ass =Asset.objects.all()
ids_list =[ i.inner_ip for i in ass]
files = range(len(ids_list))
t_list = []
t_data = []
for i in files:
t = MyThread(auto_asset, (ids_list[i],))
t_list.append(t)
t.start()
for t in t_list:
t.join() # 一定要join,不然主线程比子线程跑的快,会拿不到结果
t_data.append(t.get_result())
print(t.get_result())
执行结果
{'localhost': 'VM_75_82_centos', 'hwaddr_interfaces': '52:54:00:95:fe:c8', 'disks': {'/dev/vda1': {'total': '49.09G', 'capacity': '17%', 'avail': '38.96', 'used': '7.63'}}, 'dns': '10.236.158.106,10.236.158.114', 'ip4_interfaces': '10.105.75.82', 'num_cpus': 1, 'serialnumber': 'a1dad25c-8e69-4838-a489-0fe1958e76df', 'os': 'CentOS Linux 7 (Core)', 'mem_total': 1839, 'cpu_model': 'Intel(R) Xeon(R) CPU E5-26xx v4', 'kernelrelease': '3.10.0-514.26.2.el7.x86_64', 'virtual': 'kvm'}
{'localhost': 'wordpress', 'hwaddr_interfaces': 'fa:16:3e:1a:49:72', 'disks': {'/dev/vda1': {'total': '29.99G', 'capacity': '12%', 'avail': '26.54', 'used': '3.45'}}, 'dns': '103.224.222.222,103.224.222.223,8.8.8.8', 'ip4_interfaces': '192.168.0.3', 'num_cpus': 1, 'serialnumber': 'c6bce500-113d-11e7-a010-0425c53afb57', 'os': 'CentOS Linux 7 (Core)', 'mem_total': 1839, 'cpu_model': 'Intel Core Processor (Broadwell, IBRS)', 'kernelrelease': '3.10.0-514.el7.x86_64', 'virtual': 'kvm'}
python多线程获取子线程任务返回值的更多相关文章
- Java多线程——<四>让线程有返回值
一.概述 到目前为止,我们已经能够声明并使一个线程任务运行起来了.但是遇到一个问题:现在定义的任务都没有任何返回值,那么加入我们希望一个任务运行结束后告诉我一个结果,该结果表名任务执行成功或失败,此时 ...
- asp.net 父窗体获取子窗体的返回值,可用来对父窗体局部更新
今天在项目上遇到了这个问题,其实只是window.returnValue的简单应用,不是asp.net的专属内容.作为积累,记录一个简单的实现模型. 图1 用到的文件 从图1中我们可以看到,只用到了 ...
- 20150812 Asp.net 父窗体获取子窗体的返回值,更新父窗体文本控件(应用)
1. 父窗体 **************************************** using System;using System.Collections;using System.C ...
- python使用threading获取线程函数返回值的实现方法
python使用threading获取线程函数返回值的实现方法 这篇文章主要介绍了python使用threading获取线程函数返回值的实现方法,需要的朋友可以参考下 threading用于提供线程相 ...
- Python多线程获取返回值
在使用多线程的时候难免想要获取其操作完的返回值进行其他操作,下面的方法以作参考: 一,首先重写threading类,使其满足调用特定的方法获取其返回值 import threading class M ...
- ARTS-S 获取子线程返回值注意事项
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h& ...
- [Python]获取子线程异常信息
起因 今天在写东西的时候,用到了多线程.遇到了个问题: 子线程的异常,在父线程中无法捕获. 解决 问题代码 问题代码示例代码如下: import threading class SampleThrea ...
- C++11获取线程的返回值
C++11 std::future and std::promise 在许多时候,我们会有这样的需求--即我们想要得到线程返回的值. 但是在C++11 多线程中我们注意到,std::thread对象会 ...
- JAVA 多线程 Callable 与 FutureTask:有返回值的多线程
java多线程中,如果需要有返回值,就需要实现Callable接口. 看例子: 先建立一个Dowork这个类,就是平时某个业务的实现 package com.ming.thread.one; impo ...
随机推荐
- body标签
标签(空格分隔): body标签 body标签: 想要在网页上展示出来的内容一定要放在body标签中. 把我们之前那一段HTML代码贴过来,保存到一个HTML格式的文件中. <!DOCTYPE ...
- position:fix相对父元素定位
大家都知道,当position的值为fix时,生成绝对定位的元素,相对于浏览器窗口进行定位. 它常常应用的场合是,当下拉滚动条时固定导航栏到顶部,将广告固定在页面两侧或浏览器中间. 如果需要将导航栏d ...
- 十一、Composite 组合模式
原理: 代码清单 Entity public abstract class Entry { public abstract String getName(); public abstract int ...
- ME5xN(x:1-2): custom column is editable in Subscreen Item but not in ALV grid
FM MEMFS_BUILD_MAPPING_PO_VIEWS->LMEMFSF0Z enhancement 14 zenhance_alvg_rid_editable. "activ ...
- Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
Error starting ApplicationContext. To display the conditions report re-run your application with 'de ...
- OS history
UNIX的诞生 1965年时,贝尔实验室(Bell Labs)加入一项由奇异电子(General Electric)和麻省理工学院(MIT)合作的计划:该计划要建立一套多使用者.多任务.多层次(m ...
- sqlserver 更新通过 select 查询出的结果集
update Babies set BirthOrder =tb.sn from Babies b1, (select ROW_NUMBER() over (partition by familyid ...
- abp xunit Can not register IHostingEnvironment. It should be a non-abstract class. If not, it should be registered before.”
在测试项目的ServiceCollectionRegistrar类提前注册.
- angular模拟web API
现象:angular Cannot find module 'angular-in-memory-web-api'报错找不动“angular-in-memory-web-api”模块 解决:1.控制台 ...
- springboot restful接口服务异常处理
一.spring boot中默认的错误处理机制 二.自定义的异常处理