Search code examples
pythonasynchronousflask

How to use flask context with concurrent.futures.ThreadPoolExecutor


I'm trying to make multiple requests async and get response back, I'm using concurrent.futures to do this, but inside my function using current_app which from flask and I always got this error:

RuntimeError: Working outside of application context.

I don't know how to resolve this. Can anyone please help?

Below are my code:

run.py:

import concurrent.futures
from flask import current_app
from http_calls import get_price, get_items

def init():
    with current_app._get_current_object().test_request_context():
        with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
            futs = []
            futs.append(executor.submit(get_price))
            futs.append(executor.submit(get_items))

            print([fut.result() for fut in concurrent.futures.as_completed(futs)])

init()

http_calls.py

from flask import current_app

def get_price():
    url = current_app.config['get_price_url']
    return requests.get(url).json()

def get_items():
    url = current_app.config['get_items_url']
    return requests.get(url).json()

Solution

  • You should import your Flask instance in your script. Use current_app under the app context.

    import concurrent.futures
    from your_application import your_app  # or create_app function to return a Flask instance
    from flask import current_app
    from http_calls import get_price, get_items
    
    def init():
        with your_app.app_context():
            with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
                ...