Wait for user input via GraphQL mutation or return default value if input takes too long

I’m creating a small poker game with Python. I’m struggling with waiting for the player input (e.g., RAISE) in the main game loop, without blocking the responses to the user. If the user takes too long to take action, the game should automatically choose an action for the player (i.e., FOLD).

Strawberry GraphQL is used for all game-user connections. The user subscribes to a poker table using a mutation, the user starts the game with a mutation, the user sends their actions with a mutation, and gets the cards via a subscription, through which as the game progresses new cards are being sent (e.g., FLOP). A UserService class coordinates the message queues.

PlayerAction Mutation

"""
Rest of class is omitted for brevity
"""

@strawberry.mutation(description="Represents the player action")
async def player_action(
        self, info: strawberry.Info, input: PlayerActionInput
) -> PlayerActionPayload:

    user_service = UserService()
    message_queue = user_service.get_action_message_queue_by_player_id(input.player_token)
    
    try:
        message_queue.put_nowait([input.player_action, input.bet_amount])
        return PlayerActionPayload(
            code=200,
            success=True,
            message="Successfully executed player action.",
            player_credit=-1
        )
    except Exception as e:
        return PlayerActionPayload(
            code=500,
            success=False,
            message=str(e),
            player_credit=-1
        )

DealCards Subscription

"""
Rest of class is omitted for brevity
"""

@strawberry.type
class Subscription:

    @strawberry.subscription
    async def get_cards(self, player_id_token: str) -> AsyncGenerator[List[str], None]:
        user_service = UserService()
        message_queue = user_service.get_card_message_queue_by_player_id(player_id_token)

        while True:
            message = await message_queue.get()
            payload = []
            for card in message:
                payload.append(Card.int_to_str(card))
            yield payload

UserService Class

I tried the following two methods:

card_message_queues: Dict[str, asyncio.Queue] = {}
action_message_queues: Dict[str, asyncio.Queue] = {}

class UserService:

    def __init__(self):
        self._card_message_queues_by_player_id: Dict[str, asyncio.Queue] = card_message_queues
        self._action_queues_by_player_id: Dict[str, asyncio.Queue] = action_message_queues

    def send_cards_to_clients(self, phase: GamePhases, players: List[Player], cards):

        match phase:
            case GamePhases.PREFLOP:
                for p in players:
                    self._card_message_queues_by_player_id[p.id].put_nowait(p.cards)
            case _:
                for p in players:
                    self._card_message_queues_by_player_id[p.id].put_nowait(cards)

    def fetch_player_action_from_client(self, player, possible_actions: List[PlayerAction]) -> (PlayerAction, int):

        """See below for the different approaches"""

a) Start a new event loop and wait for the future containing the player input. This did not work at all. I’m not sure why this was the case:

print("start async init")

def run_event_loop(loop):
    asyncio.set_event_loop(loop)
    loop.run_forever()

new_loop = asyncio.new_event_loop()
t = threading.Thread(target=run_event_loop, args=(new_loop,))
t.start()

print("started thread")

future = asyncio.run_coroutine_threadsafe(self._action_queues_by_player_id[player.id].get(), new_loop)
result = future.result()
print("waited for thread")
print(result)

return PlayerAction.FOLD, 0

b) Sleep the main thread and wait this way for the user action to arrive in the message queue. This kind of worked, as the player input was registered and passed on to the main game loop. The problem here was that the GraphQL response would only send after the loop to check for the player input has run:

def fetch_player_action_from_client(self, player, possible_actions: List[PlayerAction]) -> (PlayerAction, int):
    result = None
    q = self._action_queues_by_player_id[player.id]
    for i in range(20):
        print(f"trying to fetch {player.id} action")
        time.sleep(1)

        try:
            result = q.get_nowait()
            break
        except:
            print("queue empty")

    print(result)

    return PlayerAction.FOLD, 0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật