Suppose I need to request multiple servers to make a response
def view_or_viewset(request):
d1 = request_a_server() # something like requests.get(url, data)
d2 = request_b_server()
d3 = request_c_server()
d4 = do_something_with(d3)
return Response({"foo1": d1, "foo2": d2, "foo3": d3, "foo4": d4})
I'm doing synchronous requests for each request, and I guess there must be a better way of handling this kind of scenario..
(I would use celery if it is long task, but it is not, still doing multiple synchronous requests doesn't seem right)
What's the recommended paradigm (?) of handling this?
- edit
I was expecting use of async
or aioHttp
and yield
(?)
and my question was flagged with possible duplicates and the answers there suggest using threading.. I think manually handling threading is something to avoid (from my past experience with multi threading in c++)
then I found https://stackoverflow.com/a/23902034/433570 request-future seems to be promising here..