Skip to content

Commit 1c2ca37

Browse files
author
taras
committed
Add example script
1 parent 0432809 commit 1c2ca37

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

eager_future.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from functools import partial, Placeholder as _
2+
3+
import asyncio
4+
5+
print(asyncio.Future) # Will show if it's the C or Python version
6+
print(asyncio.Task)
7+
8+
def callback_no_exc(fut, idx):
9+
print(f"callback_no_throw: {idx}")
10+
11+
12+
def callback_with_exc(fut):
13+
print("callback_with_exception")
14+
raise RuntimeError("TEST EXCEPTION")
15+
16+
17+
async def func(fut):
18+
try:
19+
print("func: entered")
20+
await fut
21+
print("func: done")
22+
except Exception as exc:
23+
print(f"func: EXCEPTION: {exc}")
24+
25+
26+
async def main():
27+
loop = asyncio.get_running_loop()
28+
loop.set_task_factory(asyncio.eager_task_factory)
29+
fut = asyncio.Future(loop=loop)
30+
fut.add_done_callback(partial(callback_no_exc, _, 1))
31+
fut.add_done_callback(callback_with_exc)
32+
fut.add_done_callback(partial(callback_no_exc, _, 2))
33+
loop.create_task(func(fut))
34+
await asyncio.sleep(1)
35+
print("main: before set_result")
36+
fut.eager_set_result(None)
37+
print("main: set_result done")
38+
await asyncio.sleep(1)
39+
print("main: done")
40+
41+
42+
asyncio.run(main())

0 commit comments

Comments
 (0)