0

I am running a http server to handle slack API calls which arrive at the events endpoint. The slack documentation suggests that the app (this server) needs to acknowledge the receipt of the request within 3 seconds (which can be achieved by an empty body and status=200). My processing of the request takes way more than 3 seconds so I want to return immediately as soon as I receive the request with a blank response and then process the data. This is the code I am using:

from aiohttp import web

routes = web.RouteTableDef()
app = web.Application()


async def process(data):
    # Do Some Processing which takes more than 5 seconds


@routes.post('/events')
async def handle_event(request):
    post_data = await request.json()
    await process(post_data)

    return web.Response(body="", status=200) 
    # I want to do this immediately then process() data afterwards

app.add_routes(routes)
web.run_app(app, port=3000)

How do I return immediately and then process the data (which includes some database queries and sending a slack message using the API, too)

I tried creating a asyncio task but the return does not happen until this task finishes executing (which is understandable):

    process_task = asyncio.create_task(process)
    await task

Akshay Shah
  • 704
  • 4
  • 11

0 Answers0