1

I have a small Django app that is highly dependent on other services. In one situation, the client makes one request to our app, and the Django app makes 3 or 4 requests to another server, aggregates the data, and saves it. This is a slow process as I need to make these calls synchronously although there is no requirement for the requests to be made sequentially.

Given the above, this particular set of calls seem like a good candidate for async programming and the asyncio package is provided with newer Python versions.

I’ve implemented what I have described using loop.run_until_complete and hooked it into a view and it seems to work fine, as well as provide a significant performance increase.

However, I am worried that I am missing something in the magic. Doing multiple google searches on whether this is a good idea in Django has given me a mixed bag of answers and I’m still unclear on the negatives of doing so.

I realize this could all be done by JavaScript client in a well supported manner, I’m just trying to reduce complexity on that side, and am more interested in the implications of doing this running Django in prod environment with WSGI.

2
  • 2
    I've used this approach in one of my projects a couple of years ago and it's been running in production ever since without problems. I've been googling a lot at that time, same as you, and eventually decided that this is a completely valid use case.
    – Borut
    Jun 21, 2018 at 11:59
  • I see no problem whatsoever in using run_until_complete in this manner. That's why it's there, after all, and Django couldn't care less how you obtained your response. Jun 21, 2018 at 19:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.