Jul-06-2021, 09:26 AM
Hi everyone!
I have test project:
But I was expecting another result.
I have test project:
from starlette.applications import Starlette from starlette.responses import JSONResponse from starlette.routing import Route from starlette.requests import Request from starlette.background import BackgroundTasks, BackgroundTask def parse_json(json_data): print(json_data['value']) async def testbground(request: Request): try: json_data = await request.json() except JSONDecodeError: print('cannot_parse_request_body') raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail='cannot_parse_request_body') except: return JSONResponse({"status": "Error"}, status_code=500) if 'value' in json_data.keys(): tasks.add_task(parse_json, json_data) else: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail='wrong_json_format') return JSONResponse({"status": "OK"}, background=tasks, status_code=200) tasks = BackgroundTasks() app = Starlette(debug=True, routes=[ Route('/testbground', endpoint=testbground, methods=['POST']), ])And have sender.py for tests:
import requests url = 'http://localhost:8000/testbground' for i in range(10): data = {'value': i} try: r = requests.post(url, json=data, timeout=5) except Exception as e: print(e)Result:
Output: 0
0 1
0 1 2
0 1 2 3
...
0 1 2 3 4 5 6 7 8 9
But I was expecting another result.
Output: 0
1
2
...
9
What is wrong? How can i add tasks to pull right?