0

I have an anchor tag that hits a route which generates a report in a new tab. I am lazyloading the report specs because I don't want to have copies of my data in the original place and on the report object. But collecting that data takes 10-20 seconds.

from flask import render_template

@app.route('/report/')
@app.route('/report/<id>')
def report(id=None):
    report_specs = function_that_takes_20_seconds(id)
    return render_template('report.html', report_specs=report_specs)

I'm wondering what I can do so that the server responds immediately with a spinner and then when function_that_takes_20_seconds is done, load the report.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
Iluvatar14
  • 699
  • 1
  • 5
  • 18

1 Answers1

0

You are right: a HTTP view is not a place for a long running tasks.

You need think your system architecture: what you can prepare outside the view and what computations must happen real time in a view.

The usual solutions include adding asynchronous properties and processing your data in a separate process. Often people use schedulers like Celery for this.

  • Prepare data in a scheduled process which runs for e.g. every 10 minutes

  • Cache results by storing them in a database

  • HTTP view always returns the last cached version

This, or then make a your view to do an AJAX call via JavaScript ("the spinner approach"). This requires obtaining some basic JavaScript skills. However this doesn't make the results appear to an end user any faster - it's just user experience smoke and mirrors.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435