0

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?

shackra
  • 277
  • 3
  • 16
  • 56
  • Does the game_loop run on a different process? (It looks as if you are using multiprocessing). – pgjones Jun 01 '19 at 08:23
  • @pgjones yes it does run in another process – shackra Jun 03 '19 at 04:11
  • I think you are better off doing the calculations on another process and passing the results extra back to be sent over the connection. I'm not sure how ContextVars work across processes (and they need to for this to work). – pgjones Jun 04 '19 at 18:09

0 Answers0