11import asyncio
22
3+
4+ def main ():
5+ loop = asyncio .get_event_loop ()
6+
7+ # Initialize components
8+ hisayer = HiSayer ()
9+ loop .create_task (hisayer .run ())
10+
11+ splitter = StringSplitter ()
12+ loop .create_task (splitter .run ())
13+
14+ lowercaser = LowerCaser ()
15+ loop .create_task (lowercaser .run ())
16+
17+ uppercaser = UpperCaser ()
18+ loop .create_task (uppercaser .run ())
19+
20+ stringjoiner = StringJoiner ()
21+ loop .create_task (stringjoiner .run ())
22+
23+ printer = Printer ()
24+ loop .create_task (printer .run ())
25+
26+ # Connect network
27+ splitter .in_lines = hisayer .out_lines
28+ lowercaser .in_lines = splitter .out_leftpart
29+ uppercaser .in_lines = splitter .out_rightpart
30+ stringjoiner .in_leftpart = lowercaser .out_lines
31+ stringjoiner .in_rightpart = uppercaser .out_lines
32+ printer .in_lines = stringjoiner .out_lines
33+
34+ # Run the full event loop
35+ loop .run_until_complete (printer .run ())
36+
37+
338class HiSayer :
439 out_lines = asyncio .Queue ()
540
641 async def run (self ):
742 for i in range (20 ):
8- self .out_lines .put_nowait (f' Hi hi for the { i + 1 } :th time...' )
43+ self .out_lines .put_nowait (f" Hi hi for the { i + 1 } :th time..." )
944 self .out_lines .task_done ()
1045
1146
@@ -17,8 +52,9 @@ class StringSplitter:
1752 async def run (self ):
1853 while not self .in_lines .empty ():
1954 s = await self .in_lines .get ()
20- self .out_leftpart .put_nowait (s [:int (len (s )/ 2 )])
21- self .out_rightpart .put_nowait (s [int (len (s )/ 2 ):])
55+ self .out_leftpart .put_nowait (s [: int (len (s ) / 2 )])
56+ self .out_rightpart .put_nowait (s [int (len (s ) / 2 ) :])
57+
2258
2359class LowerCaser :
2460 in_lines = asyncio .Queue ()
@@ -29,6 +65,7 @@ async def run(self):
2965 s = await self .in_lines .get ()
3066 self .out_lines .put_nowait (s .lower ())
3167
68+
3269class UpperCaser :
3370 in_lines = asyncio .Queue ()
3471 out_lines = asyncio .Queue ()
@@ -38,6 +75,7 @@ async def run(self):
3875 s = await self .in_lines .get ()
3976 self .out_lines .put_nowait (s .upper ())
4077
78+
4179class StringJoiner :
4280 in_leftpart = asyncio .Queue ()
4381 in_rightpart = asyncio .Queue ()
@@ -47,49 +85,17 @@ async def run(self):
4785 while not self .in_leftpart .empty () or not self .in_rightpart .empty ():
4886 leftpart = await self .in_leftpart .get ()
4987 rightpart = await self .in_rightpart .get ()
50- self .out_lines .put_nowait (f'{ leftpart } { rightpart } ' )
88+ self .out_lines .put_nowait (f"{ leftpart } { rightpart } " )
89+
5190
5291class Printer :
5392 in_lines = asyncio .Queue ()
5493
5594 async def run (self ):
5695 while not self .in_lines .empty ():
5796 s = await self .in_lines .get ()
58- print (f'Printer got line: { s } ' )
59-
60- def main ():
61- loop = asyncio .get_event_loop ()
62-
63- # Initialize components
64- hisayer = HiSayer ()
65- loop .create_task (hisayer .run ())
66-
67- splitter = StringSplitter ()
68- loop .create_task (splitter .run ())
69-
70- lowercaser = LowerCaser ()
71- loop .create_task (lowercaser .run ())
72-
73- uppercaser = UpperCaser ()
74- loop .create_task (uppercaser .run ())
75-
76- stringjoiner = StringJoiner ()
77- loop .create_task (stringjoiner .run ())
78-
79- printer = Printer ()
80- loop .create_task (printer .run ())
81-
82- # Connect network
83- splitter .in_lines = hisayer .out_lines
84- lowercaser .in_lines = splitter .out_leftpart
85- uppercaser .in_lines = splitter .out_rightpart
86- stringjoiner .in_leftpart = lowercaser .out_lines
87- stringjoiner .in_rightpart = uppercaser .out_lines
88- printer .in_lines = stringjoiner .out_lines
89-
90- # Run the full event loop
91- loop .run_until_complete (printer .run ())
97+ print (f"Printer got line: { s } " )
9298
9399
94- if __name__ == ' __main__' :
100+ if __name__ == " __main__" :
95101 main ()
0 commit comments