This repository was archived by the owner on Feb 20, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import asyncio
2+
3+ import cli_ui
4+
5+
6+ async def long_computation ():
7+ # Simulates a long computation
8+ await asyncio .sleep (0.6 )
9+
10+
11+ async def count_down (lock , start ):
12+ x = start
13+ while x >= 0 :
14+ async with lock :
15+ # Note: the sleeps are here so that we are more likely to
16+ # see mangled output
17+ #
18+ # In reality, if you only call `ui.info()` once you don't
19+ # need locks at all thanks to the GIL
20+ cli_ui .info ("down" , end = " " )
21+ await asyncio .sleep (0.2 )
22+ cli_ui .info (x )
23+ await asyncio .sleep (0.2 )
24+ await long_computation ()
25+ x -= 1
26+
27+
28+ async def count_up (lock , stop ):
29+ x = 0
30+ while x <= stop :
31+ async with lock :
32+ cli_ui .info ("up" , end = " " )
33+ await asyncio .sleep (0.2 )
34+ cli_ui .info (x )
35+ await asyncio .sleep (0.2 )
36+ await long_computation ()
37+ x += 1
38+
39+
40+ async def main ():
41+ lock = asyncio .Lock ()
42+ await asyncio .gather (count_down (lock , 4 ), count_up (lock , 4 ))
43+
44+
45+ if __name__ == "__main__" :
46+ asyncio .run (main ())
Original file line number Diff line number Diff line change 1+ import threading
2+ import time
3+ from threading import Thread
4+
5+ import cli_ui
6+
7+
8+ def long_computation ():
9+ # Simulates a long computation
10+ time .sleep (0.6 )
11+
12+
13+ def count_down (lock , start ):
14+ x = start
15+ while x >= 0 :
16+ with lock :
17+ # Note: the sleeps are here so that we are more likely to
18+ # see mangled output
19+ #
20+ # In reality, if you only call `ui.info()` once you don't
21+ # need locks at all thanks to the GIL
22+ cli_ui .info ("down" , end = " " )
23+ time .sleep (0.2 )
24+ cli_ui .info (x )
25+ time .sleep (0.2 )
26+ long_computation ()
27+ x -= 1
28+
29+
30+ def count_up (lock , stop ):
31+ x = 0
32+ while x <= stop :
33+ with lock :
34+ cli_ui .info ("up" , end = " " )
35+ time .sleep (0.2 )
36+ cli_ui .info (x )
37+ time .sleep (0.2 )
38+ long_computation ()
39+ x += 1
40+
41+
42+ def main ():
43+ lock = threading .Lock ()
44+ t1 = Thread (target = count_down , args = (lock , 4 ))
45+ t2 = Thread (target = count_up , args = (lock , 4 ))
46+
47+ t1 .start ()
48+ t2 .start ()
49+
50+ t1 .join ()
51+ t2 .join ()
52+
53+
54+ if __name__ == "__main__" :
55+ main ()
You can’t perform that action at this time.
0 commit comments