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

yipeiwu_com6年前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比较2个时间大小的实现方法

Python中有time和datetime,不过二者都直接取出日期和时间。 当需要比较2个时间的先后时,这两个类的函数都显得有些过于复杂。因为它们都带上了日期。 如果仅想比较时间,取出当...

Python使用正则实现计算字符串算式

在Python里面其实有一种特别方便实用的直接计算字符串算式的方法 那就是eval() s = '1+2*(6/2-9+3*(3*9-9))' print(eval(s)) #97....

用Python抢过年的火车票附源码

用Python抢过年的火车票附源码

前言:大家跟我一起念,Python大法好,跟着本宝宝用Python抢火车票 首先我们需要splinter 安装: pip install splinter -i http://pyp...

python计数排序和基数排序算法实例

一、计数排序 计数排序(Counting sort)是一种稳定的排序算法 算法的步骤如下:找出待排序的数组中最大和最小的元素统计数组中每个值为i的元素出现的次数,存入数组C的第i项对所有...

python计算两个地址之间的距离方法

我们调用高德地图的API来计算经纬度 #计算地址经纬度 import requests def geocode(address): parameters = {'address':...