python从子线程中获得返回值的方法

yipeiwu_com5年前Python基础

如下所示:

# coding:utf-8
import time
 
from threading import Thread
 
def foo(number):
  time.sleep(20)
  return number
 
class MyThread(Thread):
 
  def __init__(self, number):
    Thread.__init__(self)
    self.number = number
 
  def run(self):
    self.result = foo(self.number)
 
  def get_result(self):
    return self.result
 
 
thd1 = MyThread(3)
thd2 = MyThread(5)
thd1.start()
thd2.start()
thd1.join()
thd2.join()
 
print thd1.get_result()
print thd2.get_result()

以上这篇python从子线程中获得返回值的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 接口实现 供第三方调用的例子

python 接口实现 供第三方调用的例子

实验环境 1.环境问题 python 2.7 以上自带的pyunit bottle 作为一个python的简易服务器 在python安装目录 打开命令窗口(具体 shift+鼠标右键)...

对Tensorflow中权值和feature map的可视化详解

对Tensorflow中权值和feature map的可视化详解

前言 Tensorflow中可以使用tensorboard这个强大的工具对计算图、loss、网络参数等进行可视化。本文并不涉及对tensorboard使用的介绍,而是旨在说明如何通过代...

Pytorch: 自定义网络层实例

自定义Autograd函数 对于浅层的网络,我们可以手动的书写前向传播和反向传播过程。但是当网络变得很大时,特别是在做深度学习时,网络结构变得复杂。前向传播和反向传播也随之变得复杂,手动...

OpenCV 轮廓检测的实现方法

OpenCV 轮廓检测的实现方法

轮廓概述 轮廓可以简单认为成将连续的点(连着边界)连在一起的曲线,具有相同的颜色或者灰度。轮廓在形状分析和物体的检测和识别中很有用。  为了更加准确,要使用二值化图像...

python内置函数sorted()用法深入分析

本文实例讲述了python内置函数sorted()用法。分享给大家供大家参考,具体如下: 列表对象提供了sort()方法支持原地排序,而内置函数sorted()不支持原地操作只是返回新的...