Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking the progress of a function in another function

I have a Python back end running Flask, and it is going to have a function (or a few functions chained together) which will run several AJAX calls and perform some database operations.

This will take a while, so on the front end I'm looking to poll the server at regular intervals and update the UI as progress is made. A general outline might be something like this:

app.route('/update', methods=['GET'])
def getUpdate():
    # return a response with the current status of the update

@app.route('/update', methods=['POST'])
def runUpdate():
    # asynchronously call update() and return status

def update():
    # perform ajax calls
    # update database
    # query database
    # ...

I considered WebSockets, but I don't know if that's making things a little too complex for just a simple update in the UI. I know I could also use a module-scoped variable or store the status in a database table, but either of those feels like bad design to me. Is there a simple pattern I can use to achieve this?

like image 238
MattDs17 Avatar asked Oct 28 '25 09:10

MattDs17


1 Answers

Use a database to store the status. If you use something like redis, you can even do it realtime with pub/sub and websockets.

The module-scoped variable is a bad choice. It doesn't scale.

If it is a long running task, consider using a task queue, like rq or celery.

like image 191
iurisilvio Avatar answered Oct 31 '25 05:10

iurisilvio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!