-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample4.py
More file actions
30 lines (20 loc) · 670 Bytes
/
Copy pathexample4.py
File metadata and controls
30 lines (20 loc) · 670 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
from concurrent.futures import Future
import threading
def network_request_async(number):
future = Future()
result = {"success": True, "result": number ** 2}
timer = threading.Timer(1.0, lambda: future.set_result(result))
timer.start()
return future
def fetch_square(number):
fut = network_request_async(number)
def on_done_future(future):
response = future.result()
if response["success"]:
print("Result is: {}".format(response["result"]))
fut.add_done_callback(on_done_future)
if __name__ == "__main__":
### First try
fut = network_request_async(2)
### Second try
# fetch_square(2)