0

How should I trigger async functions during django/flask views?

In node.js, I can easily bypass an async function, like so:

module.exports.myView = function (request, response) {
    myVerySlowFunction(function (nonsense) {
        console.log('handle triggered nonsense', nonsense);
    });
    response.json({ message: 'ok' });
}

This is very typical to do in node, but I've never seen it done in django. I'm wanting to translate it like so:

def my_view(request):
    apply_async(my_very_slow_function)
    return Response({'message': 'okay'})

I have used Celery in the past for this, but it feels like overkill to have a separate server running just so I can trigger async functions.

I know I can trigger async functions like so: https://stackoverflow.com/a/1239252/4637643, but have never seen them in the context of a web application.

Is this a bad idea? should I look for another solution?

Red Twoon
  • 685
  • 4
  • 14

2 Answers2

0
  1. Please check django channels, This library is written fully asyncrously and is ready to shipped to Django in the future, it is a quite different implementation and not compatable with the current wsgi environment. The asyncrous consumer handle the incoming messages under the event loop implemented using asyncio and you have to run the application in an asgi server, you can check the documentation for more detail.

2.If you want to implement aync functionality in syncronous world yourself, in my humble opinion, maybe you can also start a new thread or process running a while loop continously checking tasks and register the background tasks just like celery. more details pls read 11.12 understanding Event-driven IO from python cookbook

minglyu
  • 2,958
  • 2
  • 13
  • 32
0

If you are familiar with Flask, you could move to Quart, the "async version of Flask". Quart natively supports Python's async feature while Flask doesn't. Also, it supports most of existing Flask plugins.

Here's the hello-world example copied from it's documentation. See how it's similar to Flask.

from quart import Quart

app = Quart(__name__)

@app.route('/')
async def hello():
    return 'hello'

app.run()

Quart itself is not often listed on top of asynchronous Python web frameworks. There're Sanic, FastAPI, Aiohttp, etc. But they all need some learning time. So if you're hurrying to use async programming style, try Quart.

DumTux
  • 668
  • 1
  • 9
  • 24
  • I'd say that Sanic is equivalently simple, very Flask-like API. The biggest differences between Quart and Sanic are that Quart mirrors Flask 1:1 (except for `await` sometimes) and that Sanic runs plain Python apps faster than anything else. And to original question: stepping away from Django is hard but if one is looking for high performance async functionality, Django or Flask won't do. OTOH, there are no good async ORMs (like Django's), so that is certainly a compromise if you use SQL. – Tronic Apr 13 '20 at 08:33
  • Agreed. I know Sanic or FastAPI is faster than Quart. The only better thing of Quart is that it can be used with Flask plugins directly while others couldn't. I personally use Quart when I'm gonna transform the existing Flask app to the async version. – DumTux Apr 13 '20 at 12:53