Python Flask returns html page while executing a function

I am currently building a web application using Python Flask, and I came across a road block, and I'm not sure if I think about it correctly.

So, my website homepage is just a text input landing page that is required to serve as websites. What I'm trying to accomplish is getting the web application to do two things after entering text. Firstly, the server accepts the input username and performs a function that returns nothing to the user, but creates a bunch of data that is registered in the sqlite database and used later in the process. The server then returns the web page for the survey, which should be done after entering the username. However, the function that the server performs can take up to 2 minutes, depending on the user. The way I currently encoded it, the server performs this function, and then, once it's finished, it returns a web page,therefore, the user is stuck on the loading screen for 2 minutes.

@app.route("/survey")
def main(raw_user):
    raw_user = request.args.get("SteamID")      <
    games = createGameDict(user_obj)            <----- the function
    tag_lst = get_tags(games)                   <
    return render_template("survey_page.html")

Since polling does not depend on user input, instead of having the user sitting on the loading screen, I would like them to be able to start polling when functions are running in the background, is this possible, and how would I do it?

+4
source share
1 answer

For more complex background tasks, something like celery is best suited. However, for simpler applications, you need a module threading.

Consider the following example:

from flask import Flask
from time import sleep

app = Flask(__name__)


def slow_function(some_object):
    sleep(5)
    print(some_object)

@app.route('/')
def index():
    some_object = 'This is a test'
    slow_function(some_object)
    return 'hello'

if __name__ == '__main__':
    app.run()

slow_function(), . , . http://127.0.0.1:5000 , , , .

slow_function() . , threading, :

from flask import Flask
from time import sleep
from threading import Thread

app = Flask(__name__)


def slow_function(some_object):
    sleep(5)
    print(some_object)

@app.route('/')
def index():
    some_object = 'This is a test'
    thr = Thread(target=slow_function, args=[some_object])
    thr.start()
    return 'hello'

if __name__ == '__main__':
    app.run()

, , . Thread : target, , , args, (), . , slow_function , - - , Thread. args, . , , args , .

, , thr.start(). , , . , , .

- , , , , . , ? . , "" - , , .

- , :

from flask import Flask
from time import sleep
from threading import Thread

app = Flask(__name__)


def slow_function(some_object):
    sleep(5)
    print(some_object)

def async_slow_function(some_object):
    thr = Thread(target=slow_function, args=[some_object])
    thr.start()
    return thr

@app.route('/')
def index():
    some_object = 'This is a test'
    async_slow_function(some_object)
    return 'hello'

if __name__ == '__main__':
    app.run()

async_slow_function() , - . , . , - , , , , - .

+3

Source: https://habr.com/ru/post/1682457/


All Articles