Future neste exericio é completamente desnecessário, já que é possivel alcançar o objetivo sem utiliza-lo. Por tanto para fazer uso do Future de forma que faça sentido, eu fiz que todos os resultados são apresentados novamente em forma de lista no final do programa.
import random
import asyncio
matches = [
{"id": 1, "teams": "STK1 vs Cloud9"},
{"id": 2, "teams": "100Thieves vs Disguised"},
{"id": 3, "teams": "G2 Esports vs Team Liquid"}
]
async def match_result(match, future):
print(f"Bet on match {match['id']} ({match['teams']}) registered! Awaiting results...")
if match['id'] == len(matches):
print("\n")
time = random.randint(2,3)
await asyncio.sleep(time)
teams = match['teams'].split('vs')
result = random.choice([f"Victory for {teams[0].strip()}", f"Victory for {teams[1].strip()}", f"Tie! No winner between {match['teams']}."])
future.set_result(result)
print(f"Bets results are in for match {match['id']}, victory for team: {result} (after {time} seconds)")
async def main():
futures = [asyncio.Future() for _ in matches]
tasks = [asyncio.create_task(match_result(matches[i], futures[i])) for i in range(len(matches))]
await asyncio.gather(*tasks)
print(f"\nAll matches concluded. All bet results are in:")
for r in futures:
r = str(r)
r = r.replace("<Future finished result='","")
r = r.replace("'>","")
print("-", r)
await main() # asyncio.run(main()) in regular python
Estou usando Jupyter Notebook por isso o codigo para executar a 'main()' é diferente.