Hello,
I run Django using sync workers on gunicorn. In one of my views, I make two serial calls to a third-party service:
def view():
response1 = s3.get_object(...) # takes 100 ms
response2 = s3.get_object(...) # takes 100 ms
# do something with results, and return
return JsonResponse({"result": ...})
Each call takes about 100 ms, so the view waits for 200ms total. I would like to speed this up by making the two calls concurrently. This is a classic IO problem solved in Python by either (a) an async event loop or (b) threads.
In this question on stackoverflow, I found examples of people doing both of these things inside a django view. One user made an async event loop right inside the view. The async loop is instantiated, ran, and terminated inside the context of the view. Another user reported using ThreadPoolExecutor.
Aside from the fact that this looks dirty, is this safe to do inside the synchronous view? The code example is as simple as it appears. There is no shared state, no writes, no database calls. Just trying to make 2 requests concurrently to shave off 100ms of latency.