Skip to content

Commit fa49ef6

Browse files
committed
Barrier demo
1 parent cd80bf0 commit fa49ef6

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import asyncio
2+
3+
4+
def main():
5+
print("Running game loop!")
6+
print()
7+
asyncio.run(game_loop())
8+
9+
10+
async def game_loop():
11+
barrier = asyncio.Barrier(3)
12+
13+
while True:
14+
print('/////////////// FRAME START ////////////////////////', flush=True)
15+
async with asyncio.TaskGroup() as tg:
16+
tg.create_task(compute_physics(barrier))
17+
tg.create_task(get_input(barrier))
18+
tg.create_task(render_scene(barrier))
19+
20+
print(r'\\\\\\\\\\\\\\\\\\\\\\ FRAME END \\\\\\\\\\\\\\')
21+
print()
22+
23+
24+
async def render_scene(b: asyncio.Barrier):
25+
print("Rendering scene!")
26+
# Simulate work ;)
27+
await asyncio.sleep(1.5)
28+
29+
await b.wait()
30+
31+
# Clean up work after all tasks are done.
32+
print("Rendering done.")
33+
await asyncio.sleep(.25)
34+
35+
36+
async def compute_physics(b: asyncio.Barrier):
37+
print("Computing physics!")
38+
await asyncio.sleep(.75)
39+
40+
await b.wait()
41+
42+
print("Physics done.")
43+
await asyncio.sleep(.25)
44+
45+
46+
async def get_input(b: asyncio.Barrier):
47+
print("Getting input!")
48+
await asyncio.sleep(1)
49+
50+
await b.wait()
51+
52+
print("Input done.")
53+
await asyncio.sleep(.25)
54+
55+
56+
if __name__ == '__main__':
57+
main()

0 commit comments

Comments
 (0)