看到 AsyncResult這個類,想到我之前自己寫的處理異步返回值的類
1 class MutexObject:
2 def __init__(self):
3 self.mtx = threading.Condition()
4 self.d = None
5
6 def waitObject(self,timeout):
7 d = None
8 self.mtx.acquire()
9 if self.d == None:
10 if timeout:
11 self.mtx.wait(timeout)
12 else:
13 self.mtx.wait()
14 d = self.d
15 self.d = None
16 self.mtx.release()
17 return d
18
19 def notify(self,d):
20 self.mtx.acquire()
21 self.d = d
22 self.mtx.notify()
23 self.mtx.release()
2 def __init__(self):
3 self.mtx = threading.Condition()
4 self.d = None
5
6 def waitObject(self,timeout):
7 d = None
8 self.mtx.acquire()
9 if self.d == None:
10 if timeout:
11 self.mtx.wait(timeout)
12 else:
13 self.mtx.wait()
14 d = self.d
15 self.d = None
16 self.mtx.release()
17 return d
18
19 def notify(self,d):
20 self.mtx.acquire()
21 self.d = d
22 self.mtx.notify()
23 self.mtx.release()