I am trying to use async generator and quart to stream the result of bigger queries. However I am stuck in yielding from an async function while using the request
argument of the HTTP query
from quart import request, Quart
app = Quart(__name__)
@app.route('/')
async def function():
arg = request.args.get('arg')
yield 'HelloWorld'
Start with hypercorn module:app
and calling it with curl localhost:8000/?arg=monkey
results in
[...]
File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/andre/src/cid-venv/lib/python3.8/site-packages/quart/utils.py", line 88, in _inner
return next(iterable)
File "/home/andre/src/cid/mve.py", line 7, in function
arg = request.args.get('arg')
File "/home/andre/src/cid-venv/lib/python3.8/site-packages/werkzeug/local.py", line 422, in __get__
obj = instance._get_current_object()
File "/home/andre/src/cid-venv/lib/python3.8/site-packages/werkzeug/local.py", line 544, in _get_current_object
return self.__local() # type: ignore
File "/home/andre/src/cid-venv/lib/python3.8/site-packages/quart/globals.py", line 26, in _ctx_lookup
raise RuntimeError(f"Attempt to access {name} outside of a relevant context")
RuntimeError: Attempt to access request outside of a relevant context
You will need to use the stream_with_context decorator and return a generator to achieve this, see these docs,
from quart import request, stream_with_context, Quart
app = Quart(__name__)
@app.route('/')
async def function():
@stream_with_context
async def _gen():
arg = request.args.get('arg')
yield 'HelloWorld'
return _gen()