0

我正在使用quart作为网页游戏的后端。我有一个游戏事件循环的存根和一个优先级队列,当某些事情从优先级队列中完成时,游戏循环会做进一步的处理,然后需要向一个或多个玩家发送更新。这是游戏事件循环的简化版本:

async def game_loop(
    pq_event: multiprocessing.connection.Connection,
    ws_event: multiprocessing.connection.Connection,
):
    while True:
        # FIXME: This may block the concurrency
        ready, _, _ = select([pq_event, ws_event], [], [])
        which = random.choice(ready)
        if which == pq_event:
            event = pq_event.recv()
        elif which == ws_event:
            event = ws_event.recv()

        # for now all events are printed
        print(event)

我认为装饰game_loop可以quart.copy_current_websocket_context让我使用全局 websocket 对象向玩家发送东西,但事实并非如此:

Traceback (most recent call last):
  File "/usr/local/bin/backend-server", line 3, in <module>
    from backend import main
  File "/usr/local/lib/python3.7/site-packages/backend/main.py", line 7, in <module>
    from .loop import setup_pqueue, run_reactor, game_loop
  File "/usr/local/lib/python3.7/site-packages/backend/loop.py", line 53, in <module>
    ws_event: multiprocessing.connection.Connection,
  File "/usr/local/lib/python3.7/site-packages/quart/ctx.py", line 299, in copy_current_websocket_context
    raise RuntimeError('Attempt to copy websocket context outside of a websocket context')
RuntimeError: Attempt to copy websocket context outside of a websocket context

确实,我使用了错误的装饰器,但是我想要实现的是使用 websocket 向玩家发送消息,知道我需要做什么吗?

4

0 回答 0