我使用quart作为网络游戏的后端。我有一个 game事件循环的存根和一个优先级队列,当优先级队列中的内容完成时,游戏循环将进行进一步处理,然后需要向玩家发送更新。这是游戏事件循环的简化版本:
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向玩家发送消息,对我需要做什么有所了解?