None
が返されるのは、何も返さないためです(decorator
にはreturn文がありません)。 join()
はdocumentationのように常にNone
を返します。
スレッドとの通信方法の例については、this emailを参照してください。
join()
が呼び出し元のスレッドをブロックしているので、ここで取得するのは何ですか?
編集:私は少し周りを演奏し、次のキューを必要としない解決策である(それはよりよい解決策だとは言わないだけ異なる。):
import threading
# Callable that stores the result of calling the given callable f.
class ResultCatcher:
def __init__(self, f):
self.f = f
self.val = None
def __call__(self, *args, **kwargs):
self.val = self.f(*args, **kwargs)
def threaded(f):
def decorator(*args,**kargs):
# Encapsulate f so that the return value can be extracted.
retVal = ResultCatcher(f)
th = threading.Thread(target=retVal, args=args)
th.start()
th.join()
# Extract and return the result of executing f.
return retVal.val
decorator.__name__ = f.__name__
return decorator
@threaded
def add_item(a, b):
return a + b
print(add_item(2, 2))
に見えますあなたがスレッドを開始してそれに参加するように。これは、add_item()関数を直接呼び出すのと同じです。 –