Hi,
I was wondering if it were possible to run a long process without waiting on it, and returning a basic response such as “202 - Accepted” to the client.
I thought about create_task
from Python’s Asyncio, but from my understanding, the running task could be killed by the garbage collector since the function has finished and we are not waiting for the task to complete.
Here’s what would be a long computed task:
async def long_process(request):
await asyncio.sleep(600)
@app.post('/images/convert')
async def convert_images(request):
task = asyncio.create_task(long_process(request))
return text('Accepted', status=202)
Another ideal solution would be to send the response, THEN process the data, something like :
@app.post('/images/convert')
async def convert_images(request):
await sanic.response.send('Accepted', status=201)
await long_process(request)
But I’m not sure if something like this is possible with Sanic ? (the sanic.response.send
is invented here)
Thanks for your help