title: NDB Async access compatibility with Flask in Google App Engine GAE
body: <p>I have been going through the google cloud NDB Async examles tutorials</p>
  
  <p><a href="https://cloud.google.com/appengine/docs/standard/python/ndb/async" rel="nofollow noreferrer">https://cloud.google.com/appengine/docs/standard/python/ndb/async</a></p>
  
  <blockquote>
    <p>You can specify a whole WSGIApplication as ndb.toplevel. This makes
    sure that each of the WSGIApplication's handlers waits for all async
    requests before returning. (It does not "toplevel" all the
    WSGIApplication's handlers.)</p>
  </blockquote>
  
  <pre><code>app = ndb.toplevel(webapp2.WSGIApplication([('/', MyRequestHandler)]))
  </code></pre>
  
  <p>Is this same functionality compatible with Flask? For example my code</p>
  
  <pre><code>app = Flask(__name__)
  app.config.update(DEBUG = not SERVER_ISPRODUCTION)
  app = ndb.toplevel(app)
  ...
  @app.route('/test')
  def testBackfill(): 
  </code></pre>
  
  <p>Gives me error</p>
  
  <pre><code>Traceback (most recent call last):
     File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
      handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
     File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
      handler, path, err = LoadObject(self._handler)
     File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
      obj = __import__(path[0]) 
      @app.route('/test')
   AttributeError: 'function' object has no attribute 'route'
  </code></pre>
  
  <p>This error goes away when I move the toplevel back directly to the request handler.
  I feel either flask doesn't work with this functionality, or I'm doing something wrong in how I'm using toplevel.
  My intention is for each of my request handlers within my application to wait for all of my async Google DataStore calls to finish before exiting (I'm using yield statements and tasklets within my request handlers).</p>