Background tasks in flask
<p>I am writing a web application which would do some heavy work. With that in mind I thought of making the tasks as background tasks(non blocking) so that other requests are not blocked by the previous ones.</p>
<p>I went with demonizing the thread so that it doesn't exit once the main thread (since I am using <code>threaded=True</code>) is finished, Now if a user sends a request my code will immediately tell them that their request is in progress, it'll be running in the background, and the application is ready to serve other requests.</p>
<p>My current application code looks something like this:</p>
<pre><code>from flask import Flask
from flask import request
import threading

class threadClass:

    def __init__(self):
        thread = threading.Thread(target=self.run, args=())
        thread.daemon = True                       # Daemonize thread
        thread.start()                             # Start the execution

    def run(self):

         #
         # This might take several minutes to complete
         someHeavyFunction()

app = Flask(__name__)

@app.route('/start', methods=['POST'])
    try:
        begin = threadClass()
    except:
        abort(500)

    return &quot;Task is in progress&quot;

def main():
    &quot;&quot;&quot;
    Main entry point into program execution

    PARAMETERS: none
    &quot;&quot;&quot;
    app.run(host='0.0.0.0',threaded=True)

main()
</code></pre>
<p>I just want it to be able to handle a few concurrent requests (it's not gonna be used in production)</p>
<p>Could I have done this better? Did I miss anything? I was going through python's multi-threading package and found this</p>
<blockquote>
<p>multiprocessing is a package that supports spawning processes using an
API similar to the threading module. The multiprocessing package
offers both local and remote concurrency, effectively side-stepping
the Global Interpreter Lock by using subprocesses instead of threads.
Due to this, the multiprocessing module allows the programmer to fully
leverage multiple processors on a given machine. It runs on both Unix
and Windows.</p>
</blockquote>
<p>Can I demonize a process using multi-processing? How can I achieve better than what I have with threading module?</p>
<p>##EDIT</p>
<p>I went through the multi-processing package of python, it is similar to threading.</p>
<pre><code>from flask import Flask
from flask import request
from multiprocessing import Process

class processClass:

    def __init__(self):
        p = Process(target=self.run, args=())
        p.daemon = True                       # Daemonize it
        p.start()                             # Start the execution

    def run(self):

         #
         # This might take several minutes to complete
         someHeavyFunction()

app = Flask(__name__)

@app.route('/start', methods=['POST'])
    try:
        begin = processClass()
    except:
        abort(500)

    return &quot;Task is in progress&quot;

def main():
    &quot;&quot;&quot;
    Main entry point into program execution

    PARAMETERS: none
    &quot;&quot;&quot;
    app.run(host='0.0.0.0',threaded=True)

main()
</code></pre>
<p>Does the above approach looks good?</p>