如何得到python的线程的返回值

guzl86 2009-03-22 10:24:13
import os
import time
import threading
class CmdTreadClass(threading.Thread):
'''
此类的目的是创建一个cmd命令的线程
'''
__path =''#要运行的程序的地址
def __init__(self,id,path,g):
'''
启动父类线程的初始化
'''
threading.Thread.__init__(self)
self.id = id
self.__path = path
g.append(os.system(self.__path))
def run(self):
'''
用cmd命令运行测序
'''
gu = []
t = CmdTreadClass(1,'D:/test/t1/Debug/t1.exe',gu)
def func():
t.start()
print gu
print t.isAlive()

func()


还有没有其他的办法可以让func函数得到线程中os.system()的返回值?
...全文
856 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
guzhilei1986 2009-03-22
  • 打赏
  • 举报
回复
可以这样:
# coding: GB2312
'''
Created on 2009-3-22

@author: guzl
'''
import os
import time
import threading
class CmdTreadClass(threading.Thread):
'''
此类的目的是创建一个cmd命令的线程
'''
__path =''#要运行的程序的地址
__result = 0
def __init__(self,id,path):
'''
启动父类线程的初始化
'''
threading.Thread.__init__(self)
self.id = id
self.__path = path
def run(self):
'''
用cmd命令运行测序
'''
self.__result = os.system(self.__path)
def get_result(self):
return self.__result


def func():
t1 = CmdTreadClass(1,'D:/test/t1/Debug/t1.exe')
t2 = CmdTreadClass(1,'D:/test/t2/Debug/t2.exe')
t1.start()
t2.start()
while t1.isAlive():
continue
while t2.isAlive():
continue
print t1.get_result()
print t2.get_result()
print t1.get_result()
func()

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧