I have a very simple aiohttp web server:
from aiohttp import web
import time
async def hello(request):
time.sleep(2) # heavy calculations goes here
return web.Response(text="Hello, world")
app = web.Application()
app.add_routes([web.get('/search', hello)])
web.run_app(app)
If a user (in his browser) requests '/search?query=xxx' many times within a short period, I can't do my "heavy calculation" as fast as the user requests it.
I would like to cancel old requests (and send a message to the user that his request has been cancelled) and only reply to the last request with the result of the calculation.
How can I do this?
I need this for a web page with "incremental search", so the user may send requests as fast as he can type. Is this even the proper way to tackle this?