python threading模块,生成多线程之后,怎么得到线程执行完成后return出的字符串呢?
#!/usr/bin/pythonimportthreadingfromtimeimportsleep,ctimedefmyThread(tName):print"sta...
#!/usr/bin/python
import threading
from time import sleep, ctime
def myThread(tName) :
print "start", tName, "at :", ctime()
sleep(5)
print "stop", tName, "at :", ctime()
return tName," ok"
def myPrint(myStr) :
print myStr
print "start program at ", ctime()
print
threads = []
for i in range(10) :
t = threading.Thread(target=myThread, args=(i,))
threads.append(t)
for i in threads :
i.start()
for i in threads :
i.join() #i.join() 没有返回各个线程里myThread函数的return字符串
print
print "all stop at :", ctime() 展开
import threading
from time import sleep, ctime
def myThread(tName) :
print "start", tName, "at :", ctime()
sleep(5)
print "stop", tName, "at :", ctime()
return tName," ok"
def myPrint(myStr) :
print myStr
print "start program at ", ctime()
threads = []
for i in range(10) :
t = threading.Thread(target=myThread, args=(i,))
threads.append(t)
for i in threads :
i.start()
for i in threads :
i.join() #i.join() 没有返回各个线程里myThread函数的return字符串
print "all stop at :", ctime() 展开
4个回答
展开全部
你先建立一个Queue,把Queue当参数给你的myThread,然后myThread不要返回值,而是put在Queue里面。在for i in threads里用Queue.get取出来。Queue是thread safe的,可以放心用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
定义一个全局变量存放每个线程的返回值。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class myThread(threading.Thread):
def __init__(self):
self.ret = None
def start(self):
pass
def run(self):
self.ret = ...
def __init__(self):
self.ret = None
def start(self):
pass
def run(self):
self.ret = ...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询