Exemplo n.º 1
0
 def task(app: Any, rq: Request) -> None:
     """ Run the decorated route function in a new thread """
     this_task = _tasks[task_id]
     # Pretty ugly hack, but no better solution is apparent:
     # Create a fresh Flask RequestContext object, wrapping our
     # custom _RequestProxy object that can be safely passed between threads
     with RequestContext(app, rq.environ, request=rq):
         try:
             # Run the original route function and record
             # the response (return value)
             rq.set_progress_func(progress)
             this_task["rv"] = f(*args, **kwargs)
         except HTTPException as e:
             this_task["rv"] = current_app.handle_http_exception(e)
         except Exception as e:
             # The function raised an exception, so we set a 500 error
             this_task["rv"] = InternalServerError()
             if current_app.debug:
                 # We want to find out if something happened, so reraise
                 raise
         finally:
             # We record the time of the response, to help in garbage
             # collecting old tasks
             this_task["t"] = datetime.utcnow()