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框架中flask知识点总结

有很久没有更新我的博客了,在学习flask去了,别人都说flask不难,其实现在我也这么觉得,但是在刚接触的时候还是有点吃力的。 在学习的过程中查阅了不少,也了解了许多,今天想做个总结。...

Python socket套接字实现C/S模式远程命令执行功能案例

本文实例讲述了Python socket套接字实现C/S模式远程命令执行功能。分享给大家供大家参考,具体如下: 一. 前言 要求: 使用python的socket套接字编写服务器/客户...

python 动态调用函数实例解析

1. 根据字符串名称 动态调用 python文件内的方法eval("function_name")(参数) 2. 根据字符串 动态调用类中的静态方法,getattr(ClassName,...

python安装以及IDE的配置教程

python安装以及IDE的配置教程

一、初识Python   Python官方网站:www.python.org   版本:python-3.4.3.amd64   somebody初次接触Python,没有使用Pytho...

python 监听salt job状态,并任务数据推送到redis中的方法

salt分发后,主动将已完成的任务数据推送到redis中,使用redis的生产者模式,进行消息传送 #coding=utf-8 import fnmatch,json,logging...