f | import asyncio | f | import asyncio |
| import random | | import random |
| | | |
| class Seq: | | class Seq: |
n | _instance_count = 0 | n | _counter = 0 |
| _worker_tasks = [] | | _events = [] |
| | | |
| def __init__(self, name): | | def __init__(self, name): |
| self.name = name | | self.name = name |
n | self._id = Seq._instance_count | n | self.id = Seq._counter |
| Seq._instance_count += 1 | | Seq._counter += 1 |
| while len(Seq._worker_tasks) <= self._id: | | while len(Seq._events) <= self.id: |
| Seq._worker_tasks.append(asyncio.Event()) | | Seq._events.append(asyncio.Event()) |
| | | |
| async def run(self): | | async def run(self): |
n | if self._id > 0: | n | if self.id > 0: |
| await Seq._worker_tasks[self._id - 1].wait() | | await Seq._events[self.id - 1].wait() |
| print(self.name) | | print(self.name) |
t | Seq._worker_tasks[self._id].set() | t | Seq._events[self.id].set() |
| return self.name | | return self.name |