Rendezvous: Difference between revisions
Content deleted Content added
m →OpenMP implementation: Fix header level |
A Python implementation |
||
Line 1,390: | Line 1,390: | ||
2: And mounting its back, |
2: And mounting its back, |
||
Humpty Dumpty: Out of Ink!</pre> |
Humpty Dumpty: Out of Ink!</pre> |
||
=={{header|Python}}== |
|||
{{works with|Python|3.7}} |
|||
<lang python>"""An approximation of the rendezvous pattern found in Ada using asyncio.""" |
|||
from __future__ import annotations |
|||
import asyncio |
|||
import sys |
|||
from typing import Optional |
|||
from typing import TextIO |
|||
class OutOfInkError(Exception): |
|||
"""Exception raised when a printer is out of ink.""" |
|||
class Printer: |
|||
def __init__(self, name: str, backup: Optional[Printer]): |
|||
self.name = name |
|||
self.backup = backup |
|||
self.ink_level: int = 5 |
|||
self.output_stream: TextIO = sys.stdout |
|||
async def print(self, msg): |
|||
if self.ink_level <= 0: |
|||
if self.backup: |
|||
await self.backup.print(msg) |
|||
else: |
|||
raise OutOfInkError(self.name) |
|||
else: |
|||
self.ink_level -= 1 |
|||
self.output_stream.write(f"({self.name}): {msg}\n") |
|||
async def main(): |
|||
reserve = Printer("reserve", None) |
|||
main = Printer("main", reserve) |
|||
humpty_lines = [ |
|||
"Humpty Dumpty sat on a wall.", |
|||
"Humpty Dumpty had a great fall.", |
|||
"All the king's horses and all the king's men,", |
|||
"Couldn't put Humpty together again.", |
|||
] |
|||
goose_lines = [ |
|||
"Old Mother Goose,", |
|||
"When she wanted to wander,", |
|||
"Would ride through the air,", |
|||
"On a very fine gander.", |
|||
"Jack's mother came in,", |
|||
"And caught the goose soon,", |
|||
"And mounting its back,", |
|||
"Flew up to the moon.", |
|||
] |
|||
async def print_humpty(): |
|||
for line in humpty_lines: |
|||
try: |
|||
task = asyncio.Task(main.print(line)) |
|||
await task |
|||
except OutOfInkError: |
|||
print("\t Humpty Dumpty out of ink!") |
|||
break |
|||
async def print_goose(): |
|||
for line in goose_lines: |
|||
try: |
|||
task = asyncio.Task(main.print(line)) |
|||
await task |
|||
except OutOfInkError: |
|||
print("\t Mother Goose out of ink!") |
|||
break |
|||
await asyncio.gather(print_goose(), print_humpty()) |
|||
if __name__ == "__main__": |
|||
asyncio.run(main(), debug=True)</lang> |
|||
{{out}} |
|||
<pre> |
|||
(main): Old Mother Goose, |
|||
(main): Humpty Dumpty sat on a wall. |
|||
(main): When she wanted to wander, |
|||
(main): Humpty Dumpty had a great fall. |
|||
(main): Would ride through the air, |
|||
(reserve): All the king's horses and all the king's men, |
|||
(reserve): On a very fine gander. |
|||
(reserve): Couldn't put Humpty together again. |
|||
(reserve): Jack's mother came in, |
|||
(reserve): And caught the goose soon, |
|||
Mother Goose out of ink!</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |