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实现一行输入多个值和一行输出多个值的例子

python实现一行输入多个值和一行输出多个值的例子

注:以下内容在python3中操作 一. 一行输入多个值 a,b = input().split() #此时得到的a和b的类型均为字符串,以空格为分隔符 a,b = input()....

Python中endswith()函数的基本使用

函数:endswith() 作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型 相关函数:判断字符串开头 startswith() 一、函数说明 语法:string.en...

python隐藏终端执行cmd命令的方法

在用pyinstaller打包后不想要后面的终端命令框,但是打包时加了-w或者--noconsole命令后会导致cmd程序不能运行从而出错。这个时候用subprocess可以解决该类问题...

记录Django开发心得

概念层面 概括 Django是属于MVC的Web框架。 Model:负责与数据库打交道 View:负责获取或者增强从Models得到的数据 Controller:这是Django本身 P...

Python基于smtplib实现异步发送邮件服务

基于smtplib包制作而成,但在实践中发现一个不知道算不算是smtplib留的一个坑,在网络断开的情况下发送邮件时会抛出一个socket.gaierror的异常,但是smtplib中并...