Это один из способов его реализации:
import asyncio
class Player:
def __init__(self, name: str, seconds_per_turn: float):
self.name = name
self.seconds_per_turn = seconds_per_turn
self.busy = asyncio.Condition()
async def move(self, game):
await self.busy.acquire()
print(f'game {game.game_number} player {self.name} begins {self.seconds_per_turn} second move')
await asyncio.sleep(self.seconds_per_turn)
print(f'game {game.game_number} player {self.name} ends {self.seconds_per_turn} second move')
self.busy.release()
class Game:
def __init__(self, game_number: int, player1: Player, player2: Player, number_of_turns: int):
self.game_number = game_number
self.player1 = player1
self.player2 = player2
self.number_of_turns = number_of_turns
async def start(self):
print(f'game {self.game_number} started')
for turn in range(1, self.number_of_turns + 1):
print(f'game {self.game_number} turn #{turn}')
await self.player1.move(self)
await self.player2.move(self)
print(f'game {self.game_number} ended')
if __name__ == '__main__':
synchronous = False
loop = asyncio.get_event_loop()
judit = Player('judit', 5)
for i in range(1, 25):
opponent = Player(f'opponent {i}', 55)
game = Game(i, judit, opponent, 30)
if synchronous:
loop.run_until_complete(game.start())
else:
asyncio.ensure_future(game.start())
loop.run_forever()
Выход:
game 1 started
game 1 turn #1
game 1 player judit begins 5 second move
game 2 started
game 2 turn #1
game 3 started
game 3 turn #1
game 4 started
game 4 turn #1
game 5 started
game 5 turn #1
game 6 started
game 6 turn #1
game 7 started
game 7 turn #1
game 8 started
game 8 turn #1
game 9 started
game 9 turn #1
game 10 started
game 10 turn #1
game 11 started
game 11 turn #1
game 12 started
game 12 turn #1
game 13 started
game 13 turn #1
game 14 started
game 14 turn #1
game 15 started
game 15 turn #1
game 16 started
game 16 turn #1
game 17 started
game 17 turn #1
game 18 started
game 18 turn #1
game 19 started
game 19 turn #1
game 20 started
game 20 turn #1
game 21 started
game 21 turn #1
game 22 started
game 22 turn #1
game 23 started
game 23 turn #1
game 24 started
game 24 turn #1
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 2 player judit begins 5 second move
game 2 player judit ends 5 second move
game 2 player opponent 2 begins 55 second move
game 3 player judit begins 5 second move
game 3 player judit ends 5 second move
game 3 player opponent 3 begins 55 second move
game 4 player judit begins 5 second move
game 4 player judit ends 5 second move
game 4 player opponent 4 begins 55 second move
game 5 player judit begins 5 second move
game 5 player judit ends 5 second move
game 5 player opponent 5 begins 55 second move
game 6 player judit begins 5 second move
game 6 player judit ends 5 second move
game 6 player opponent 6 begins 55 second move
game 7 player judit begins 5 second move
game 7 player judit ends 5 second move
game 7 player opponent 7 begins 55 second move
game 8 player judit begins 5 second move
game 8 player judit ends 5 second move
game 8 player opponent 8 begins 55 second move
game 9 player judit begins 5 second move
game 9 player judit ends 5 second move
game 9 player opponent 9 begins 55 second move
game 10 player judit begins 5 second move
game 10 player judit ends 5 second move
game 10 player opponent 10 begins 55 second move
game 11 player judit begins 5 second move
game 11 player judit ends 5 second move
game 11 player opponent 11 begins 55 second move
game 12 player judit begins 5 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #2
game 12 player judit ends 5 second move
game 12 player opponent 12 begins 55 second move
game 13 player judit begins 5 second move
game 2 player opponent 2 ends 55 second move
game 2 turn #2
game 13 player judit ends 5 second move
game 13 player opponent 13 begins 55 second move
game 14 player judit begins 5 second move
game 3 player opponent 3 ends 55 second move
game 3 turn #2
game 14 player judit ends 5 second move
game 14 player opponent 14 begins 55 second move
game 15 player judit begins 5 second move
game 4 player opponent 4 ends 55 second move
game 4 turn #2
game 15 player judit ends 5 second move
game 15 player opponent 15 begins 55 second move
game 16 player judit begins 5 second move
game 5 player opponent 5 ends 55 second move
game 5 turn #2
game 16 player judit ends 5 second move
game 16 player opponent 16 begins 55 second move
game 17 player judit begins 5 second move
game 6 player opponent 6 ends 55 second move
game 6 turn #2
game 17 player judit ends 5 second move
game 17 player opponent 17 begins 55 second move
game 18 player judit begins 5 second move
game 7 player opponent 7 ends 55 second move
game 7 turn #2
game 18 player judit ends 5 second move
game 18 player opponent 18 begins 55 second move
game 19 player judit begins 5 second move
game 8 player opponent 8 ends 55 second move
game 8 turn #2
game 19 player judit ends 5 second move
game 19 player opponent 19 begins 55 second move
game 20 player judit begins 5 second move
game 9 player opponent 9 ends 55 second move
game 9 turn #2
game 20 player judit ends 5 second move
game 20 player opponent 20 begins 55 second move
game 21 player judit begins 5 second move
game 10 player opponent 10 ends 55 second move
game 10 turn #2
game 21 player judit ends 5 second move
game 21 player opponent 21 begins 55 second move
game 22 player judit begins 5 second move
game 11 player opponent 11 ends 55 second move
game 11 turn #2
game 22 player judit ends 5 second move
game 22 player opponent 22 begins 55 second move
game 23 player judit begins 5 second move
game 12 player opponent 12 ends 55 second move
game 12 turn #2
game 23 player judit ends 5 second move
game 23 player opponent 23 begins 55 second move
game 24 player judit begins 5 second move
game 13 player opponent 13 ends 55 second move
game 13 turn #2
game 24 player judit ends 5 second move
game 24 player opponent 24 begins 55 second move
game 1 player judit begins 5 second move
game 14 player opponent 14 ends 55 second move
game 14 turn #2
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 2 player judit begins 5 second move
game 15 player opponent 15 ends 55 second move
game 15 turn #2
game 2 player judit ends 5 second move
game 2 player opponent 2 begins 55 second move
game 3 player judit begins 5 second move
game 16 player opponent 16 ends 55 second move
game 16 turn #2
game 3 player judit ends 5 second move
game 3 player opponent 3 begins 55 second move
[...]
При установке synchrounous
на True
первая игра должна закончиться до того, как начнется вторая.
Вывод с synchrounous = True
:
game 1 started
game 1 turn #1
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #2
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #3
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #4
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #5
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #6
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #7
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #8
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #9
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #10
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #11
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #12
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #13
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #14
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #15
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #16
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #17
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #18
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #19
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #20
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #21
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #22
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #23
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #24
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #25
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #26
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #27
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #28
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #29
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #30
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 ended
game 2 started
game 2 turn #1
game 2 player judit begins 5 second move
game 2 player judit ends 5 second move
game 2 player opponent 2 begins 55 second move
game 2 player opponent 2 ends 55 second move
game 2 turn #2
game 2 player judit begins 5 second move
game 2 player judit ends 5 second move
game 2 player opponent 2 begins 55 second move
game 2 player opponent 2 ends 55 second move
game 2 turn #3
game 2 player judit begins 5 second move
[...]