title: Use Flask's "app" singleton to Dask Scheduler/Workers
body: <p><strong>The Case:</strong></p>
  
  <p>We have some time-consuming functional/integration tests that utilize Flask's <code>current_app</code> for configuration (global variables etc.) and some logging.<br/>
  We are trying to distribute and parallelize those tests on a cluster (for the moment a local "cluster" created from <a href="https://docs.dask.org/en/latest/setup/docker.html" rel="nofollow noreferrer">Dask's Docker image</a>.).</p>
  
  <p><strong>The Issue(s?):</strong> </p>
  
  <p>Let's assume the following example:</p>
  
  <p>A time-consuming function:</p>
  
  <pre><code>def will_take_my_time(n)
      # Add the 'TAKE_YOUR_TIME' in the config in how many seconds you want
      time.sleep(current_app.config['TAKE_YOUR_TIME'])
      return n
  </code></pre>
  
  <p>A time-consuming test:</p>
  
  <pre><code>def need_my_time_test(counter=None):
      print(f"Test No. {will_take_my_time(counter)}")
  </code></pre>
  
  <p>A Flask CLI command that creates a Dask <a href="https://distributed.dask.org/en/latest/client.html" rel="nofollow noreferrer"><code>Client</code></a> to connect to the cluster and execute 10 tests of <code>need_my_time_test</code>:</p>
  
  <pre><code>@app.cli.command()
  def itests(extended):
      with Client(processes=False) as dask_client:
          futures = dask_client.map(need_my_time_test, range(10))
          print(f"Futures: {futures}")
          print(f"Gathered: {dask_client.gather(futures)}")
  </code></pre>
  
  <p><strong>EDIT:</strong> For convenience let's add an application factory for an easier reproducible example:</p>
  
  <pre><code>def create_app():
      app = Flask(__name__)
      app.config.from_mapping(
          SECRET_KEY='dev',
          DEBUG=True,
      )
  
      @app.route('/hello')
      def hello():
          return 'Hello, World!'
  
      @app.cli.command()
      def itests(extended):
          with Client(processes=False) as dask_client:
              futures = dask_client.map(need_my_time_test, range(10))
              print(f"Futures: {futures}")
              print(f"Gathered: {dask_client.gather(futures)}")
  </code></pre>
  
  <p>Using the above with <code>flask itests</code>, we are running into the following error (described <a href="https://flask.palletsprojects.com/en/1.1.x/appcontext/" rel="nofollow noreferrer">here</a>):</p>
  
  <blockquote>
    <p>RuntimeError: Working outside of application context.</p>
    
    <p>This typically means that you attempted to use functionality that
    needed to interface with the current application object in some way.
    To solve this, set up an application context with app.app_context().</p>
  </blockquote>
  
  <p>We have tried:</p>
  
  <ul>
  <li>Pushing the <code>app_context</code> (<code>app.app_context().push()</code>) on the app singleton creation.</li>
  <li>Using <code>with current_app.app_context():</code> on the CLI command and some of the functions that use the <code>current_app</code>.</li>
  <li>Sending the <code>app_context</code> through a Dask <a href="https://docs.dask.org/en/latest/futures.html?highlight=Variable#global-variables" rel="nofollow noreferrer"><code>Variable</code></a> but it cannot serialize the context.</li>
  </ul>
  
  <p>With no avail.</p>
  
  <p><strong>The questions:</strong></p>
  
  <ul>
  <li>Are there any suggestions on what should we try (containing the "where" will be highly appreciated)?</li>
  <li>Is there something that we tried correct but misused and we should retry it differently?</li>
  </ul>