sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I have a question. Suppose I add the task using request.app.add_task, but it's possible that this HTTP request will time out after 60 seconds. Will it affect and interrupt the execution of the code in the task, causing it to stop abruptly?

zhugexinxin opened this issue · comments

Link to code

No response

Proposal

I have a question. Suppose I add the task using request.app.add_task, but it's possible that this HTTP request will time out after 60 seconds. Will it affect and interrupt the execution of the code in the task, causing it to stop abruptly?

Additional context

@api_v1.route('/test', methods=["PUT"])
async def test(request):
    data = request.json
    collection_name = data.get('collection_name')
    request.app.add_task(test_task(request, data), name=collection_name)
    
    return response.json({
        'success': True,
    })

I have a question. Suppose I add the task using request.app.add_task, but it's possible that this HTTP request will time out after 60 seconds. Will it affect and interrupt the execution of the code in the task, causing it to stop abruptly?

Is this a breaking change?

  • Yes

No, tasks run independently and can keep running as long as the server and thus app are running. More typically you would add them @app.before_server_start only once, and then use queues and/or asyncio.sleep for them to handle some background jobs, rather than spawn new ones in requests.

Since they are not restricted, if the tasks for some reason get hung or someone spams the server with a lot of requests, you may end up with more tasks running than you can handle (running out of memory, mostly).