Using Async Background Task Controller
Example
import asyncio
from apidaora import appdaora, route
# async tasks can potentially block yours application, use with care
# it use is recommended just for small tasks or non-blocking operations
@route.background('/hello')
async def hello_task(name: str) -> str:
await asyncio.sleep(1)
return f'Hello {name}!'
app = appdaora(hello_task)
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 task
Creating the task:
curl -X POST -i localhost:8000/hello?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?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?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}