Making asynchronous HTTP requests from a flask service Making asynchronous HTTP requests from a flask service flask flask

Making asynchronous HTTP requests from a flask service


You can use eventlets for the the second use case. It's pretty easy to do:

import eventletproviders = [EventfulPump(), MeetupPump()]try:    pool = eventlet.GreenPool()    pile = eventlet.GreenPile(pool)    for each in providers:        pile.spawn(each.get, [], 5, loc)  # call the interface methodexcept (PumpFailure, PumpOverride):    return abort(503)results = []for res in pile:    results += res

You can wrap each of your api endpoints in a class that implements a "common interface" (in the above it is the get method) and you can make the calls in parallel. I just place them all in a list.

Your other use case is harder to accomplish in straight python. At least a few years ago you would be forced to introduce some sort of worker process like celery to get something like that done. This question seems to cover all the issues:

Making an asynchronous task in Flask

Perhaps things have changed in flask land?