-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.py
More file actions
37 lines (33 loc) · 1023 Bytes
/
async.py
File metadata and controls
37 lines (33 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from threading import Thread
from time import sleep
class Async(Thread):
def __init__(self, function, *args, **kwargs):
self._callback = None
self.function = function
self.args = args
self.kwargs = kwargs
self._value = None
self.finished = False
Thread.__init__(self)
self.start()
def callback(self, _callback):
self._callback = _callback
@property
def value(self):
if not self.finished:
self.join()
return self._value
def run(self):
self._value = self.function(*self.args, **self.kwargs)
self.finished = True
if self._callback:
self._callback(self)
if __name__ == "__main__":
def slow_add(x,y):
sleep(1)
return x+y
def print_value(async):
print(async.value)
print(Async(slow_add, 1, 2).value) #blocking
print(Async(slow_add, 1, 3)) #future
Async(slow_add, 1, 4).callback(print_value) #callback