The next wrapper function will wrap the existing function and return an object that points to the stream (so you can call start() , join() , etc.), as well as access / view its possible return value.
def threadwrap(func,args,kwargs): class res(object): result=None def inner(*args,**kwargs): res.result=func(*args,**kwargs) import threading t = threading.Thread(target=inner,args=args,kwargs=kwargs) res.thread=t return res def myFun(v,debug=False): import time if debug: print "Debug mode ON" time.sleep(5) return v*2 x=threadwrap(myFun,[11],{"debug":True}) x.thread.start() x.thread.join() print x.result
It looks fine, and the threading.Thread class seems to be easily extended (*) with such functionality, so I wonder why it doesn't exist yet. Is there a flaw in the above method?
(*) Note that husanu's answer for this question does just that, a subclassification of threading.Thread leads to a version where join() gives the return value.
Andz Mar 22 '16 at 2:04 on 2016-03-22 14:04
source share