|
| 1 | +import asyncio |
| 2 | + |
| 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 | + |
| 38 | +class HiSayer: |
| 39 | + out_lines = asyncio.Queue() |
| 40 | + |
| 41 | + async def run(self): |
| 42 | + for i in range(20): |
| 43 | + await self.out_lines.put(f"Hi hi for the {i+1}:th time...") |
| 44 | + |
| 45 | + |
| 46 | +class StringSplitter: |
| 47 | + in_lines = asyncio.Queue() |
| 48 | + out_leftpart = asyncio.Queue() |
| 49 | + out_rightpart = asyncio.Queue() |
| 50 | + |
| 51 | + async def run(self): |
| 52 | + while not self.in_lines.empty(): |
| 53 | + s = await self.in_lines.get() |
| 54 | + await self.out_leftpart.put(s[: int(len(s) / 2)]) |
| 55 | + await self.out_rightpart.put(s[int(len(s) / 2) :]) |
| 56 | + |
| 57 | + |
| 58 | +class LowerCaser: |
| 59 | + in_lines = asyncio.Queue() |
| 60 | + out_lines = asyncio.Queue() |
| 61 | + |
| 62 | + async def run(self): |
| 63 | + while not self.in_lines.empty(): |
| 64 | + s = await self.in_lines.get() |
| 65 | + await self.out_lines.put(s.lower()) |
| 66 | + |
| 67 | + |
| 68 | +class UpperCaser: |
| 69 | + in_lines = asyncio.Queue() |
| 70 | + out_lines = asyncio.Queue() |
| 71 | + |
| 72 | + async def run(self): |
| 73 | + while not self.in_lines.empty(): |
| 74 | + s = await self.in_lines.get() |
| 75 | + await self.out_lines.put(s.upper()) |
| 76 | + |
| 77 | + |
| 78 | +class StringJoiner: |
| 79 | + in_leftpart = asyncio.Queue() |
| 80 | + in_rightpart = asyncio.Queue() |
| 81 | + out_lines = asyncio.Queue() |
| 82 | + |
| 83 | + async def run(self): |
| 84 | + while not self.in_leftpart.empty() or not self.in_rightpart.empty(): |
| 85 | + leftpart = await self.in_leftpart.get() |
| 86 | + rightpart = await self.in_rightpart.get() |
| 87 | + await self.out_lines.put(f"{leftpart}{rightpart}") |
| 88 | + |
| 89 | + |
| 90 | +class Printer: |
| 91 | + in_lines = asyncio.Queue() |
| 92 | + |
| 93 | + async def run(self): |
| 94 | + while not self.in_lines.empty(): |
| 95 | + s = await self.in_lines.get() |
| 96 | + print(f"Printer got line: {s}") |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments