title: Flask: What happens when a user closes the browser while a long process is being executed?
body: <p>The situation is the following:</p>
  
  <p>I have an app which uses Angularjs for the front-end and Flask for the back-end.</p>
  
  <p>And I have a route that looks like:</p>
  
  <pre><code>@app.route('/api/route1', methods=['POST'])
  def route1():
      result = some_package.long_task()
      result2 = some_package.short_task(result)
      return jsonify(result)
  </code></pre>
  
  <p>The <code>long_task</code> function is executing some bash commands using <code>check_output</code>, using the database, reading and writing files and so on. It can take a couple of hours to get finished. </p>
  
  <p>Let's say that the user gets tired of waiting and closes the browser window 10 minutes after the process started.</p>
  
  <p>My questions are:</p>
  
  <ul>
  <li>Will both <code>long_task</code> and <code>short_task</code> will be executed anyway? </li>
  <li>Is it possible that this situation creates any memory leakage?</li>
  <li>Is it possible to know that the user closed the browser at some point? (including when I try to return the response: would it be possible to know that the response wasn't delivered?)</li>
  <li>Does the answer of this question depends on what server am I using? (Tornado, uWSGI...)</li>
  </ul>
  
  <p>Thanks a lot for your answers.</p>