I need a new handler that use another existing handler for processing multiple requests at once:
async def process_multiple_queries(request):
init_request = request.clone()
body = await request.json()
for query in body["queries"]:
@asyncio.coroutine
def fake_json(self):
return query
# Replace request body on new fake json
R = init_request.clone()
R.json = fake_json.__get__(R, Request)
# Process request in the background task
asyncio.ensure_future(process_query(R))
async def process_query(request):
body = await request.json()
# ... Process body ...
return web.json_response({"result": "OK"})
However, results of all scheduled tasks are equal to the result of the last task, i.e. bodies of all requests passed in process_query
are equal only to the body of the last request.
But if I add asyncio.sleep(5)
after asyncio.ensure_future(process_query(R))
, all tasks are processed properly and produce different results.
How to fix it?