Using Background Task Controller with Lock

Example

import time

from apidaora import appdaora, route


@route.background('/hello-single', lock=True)
def hello_task(name: str) -> str:
    time.sleep(1)
    return f'Hello {name}!'


app = appdaora(hello_task)

Running

Running the server:

uvicorn myapp:app
INFO: Started server process [16220]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Creating tasks

Creating the task twice, getting error on second:

curl -X POST -i localhost:8000/hello-single?name=Me
echo && echo
curl -X POST -i localhost:8000/hello-single?name=Me
HTTP/1.1 202 Accepted
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 159

{"task_id":"4ee301eb-6487-48a0-b6ed-e5f576accfc2","start_time":"1970-01-01T00:00:00+00:00","status":"running","signature":"aedb1ee4c3c7","args_signature":null}

HTTP/1.1 303 See Other
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
location: hello-single?task_id=4ee301eb-6487-48a0-b6ed-e5f576accfc2
x-apidaora-lock: signature
transfer-encoding: chunked

Quering

Quering the server for the task (You must replace the task_id with the server output):

curl -i localhost:8000/hello-single?task_id=4ee301eb-6487-48a0-b6ed-e5f576accfc2
HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 159

{"task_id":"4ee301eb-6487-48a0-b6ed-e5f576accfc2","start_time":"1970-01-01T00:00:00+00:00","status":"running","signature":"aedb1ee4c3c7","args_signature":null}

Wait the task to finish and get results:

sleep 1 && curl -i localhost:8000/hello-single?task_id=4ee301eb-6487-48a0-b6ed-e5f576accfc2
HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 220

{"end_time":"1970-01-01T00:00:00+00:00","result":"Hello Me!","status":"finished","task_id":"4ee301eb-6487-48a0-b6ed-e5f576accfc2","start_time":"1970-01-01T00:00:00+00:00","signature":"aedb1ee4c3c7","args_signature":null}

Creating another task

Now the task should be able to run again

curl -X POST -i localhost:8000/hello-single?name=Me
HTTP/1.1 202 Accepted
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 159

{"task_id":"4ee301eb-6487-48a0-b6ed-e5f576accfc2","start_time":"1970-01-01T00:00:00+00:00","status":"running","signature":"aedb1ee4c3c7","args_signature":null}

Quering

Quering the server for the task (You must replace the task_id with the server output):

curl -i localhost:8000/hello-single?task_id=4ee301eb-6487-48a0-b6ed-e5f576accfc2
HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 159

{"task_id":"4ee301eb-6487-48a0-b6ed-e5f576accfc2","start_time":"1970-01-01T00:00:00+00:00","status":"running","signature":"aedb1ee4c3c7","args_signature":null}

Wait the task to finish and get results:

sleep 1 && curl -i localhost:8000/hello-single?task_id=4ee301eb-6487-48a0-b6ed-e5f576accfc2
HTTP/1.1 200 OK
date: Thu, 1st January 1970 00:00:00 GMT
server: uvicorn
content-type: application/json
content-length: 220

{"end_time":"1970-01-01T00:00:00+00:00","result":"Hello Me!","status":"finished","task_id":"4ee301eb-6487-48a0-b6ed-e5f576accfc2","start_time":"1970-01-01T00:00:00+00:00","signature":"aedb1ee4c3c7","args_signature":null}