Accessing shared context from custom process?

Hi everyone, i am trying to access a shared context queue from a custom process:

app = Sanic("Server")

def subprocess():
       while True:
            msg = app.shared_ctx.vjQueue.get()
            ...

@app.main_process_ready
async def launchProcesses(app):
    app.manager.manage("subprocess", subprocess)


@app.main_process_start
async def createDicts(app):
    app.shared_ctx.vjQueue= Queue()

however, this results in AttributeError: 'SharedContext' object has no attribute 'vjQueue'. Does the subprocess not have access to the shared context or what am i doing wrong?

Sorry for the late reply.

Of course you can! You just need to be explicit about it and actually pass the context object as an argument.

from multiprocessing import Queue
from queue import Empty
from time import sleep

from sanic import Request, Sanic, json


def my_process(q):
    print("Entering process")
    try:
        while True:
            try:
                print("Received:", q.get(timeout=1))
            except Empty:
                pass
            sleep(1)
    except KeyboardInterrupt:
        print("done")


app = Sanic("TestApp")


@app.get("/")
async def handler(request: Request):
    request.app.shared_ctx.queue.put(str(request.id))
    return json({"foo": "bar"})


@app.main_process_start
async def main_process_start(app):
    app.shared_ctx.queue = Queue()


@app.main_process_ready
async def ready(app: Sanic, _):
    app.manager.manage("MyProcess", my_process, {"q": app.shared_ctx.queue})