I'm using quart as the back-end of a web game. I have a stub of the game event loop and a priority queue, when something completes from the priority queue the game loop do further processing and then is required to send an update to the player or players. Here is a simplified version of the game event loop:
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)
I thought that decorating game_loop
with quart.copy_current_websocket_context
would allow me to use the global websocket object to send stuff to the players, but is not the case:
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
Indeed, I'm using wrong the decorator, however what I want to achieve is to send messages to the players with websocket, any idea of what I need to do?