Calculating Views asynchronous

Hi. I want to keep doing some calculations after a View has finished doing its calculation. For example i have this view (Differential) that shows a table

After that tab has finished and the table is show, i want to start calculating the tables in other tabs like ‘Construction’ or ‘Interaction’, but i cant because i have to return a view in Differential and then the code stop running, so i want to run the other tabs asynchronous, and when the user click the other tab, the calculation is already done.

To the “why” i would want this, is that the calculations take minutes to complete, so when you press ‘Update’ in a tab, it takes from 2 to 5 minutes to the calculations to end (These are heavy calculations), so i want to save time and start calculating other tabs when the user is doing something else.

Hi Carlos,

Thanks for your question. What you want is not possible, with the current platform design.

But maybe Memoize can help you here.
Memoize saves the result of a function with a certain input. So when the function is called with the same input, the saved result is returned and the long calculation is not done again.

In you case you can do something like this:

class Controller(ViktorController)

    @PlotlyView('Tab 1', duration_guess=4)
    def view_tab_1(self, params:Munch)

        calc_inputs = calc_input_from_params(params)  # this takes short amount of time
        result = long_calculation(calc_inputs)['result_tab1']
        fig = make_ploty_table(result)

        return PlotlyResult(fig.to_json())

    @PlotlyView('Tab 2', duration_guess=4)
    def view_tab_2(self, params: Munch)

        calc_inputs = calc_input_from_params(params) # this takes short amount of time

        result = long_calculation(calc_inputs)['result_tab2']
        fig = make_ploty_table(result)

        return PlotlyResult(fig.to_json())

    @memoize
    def long_calculation(params)

        #  do some long calculation

        return {
            'result_tab1': results_tab_1,
            'result_tab2': results_tab_2,
        }

So all results are calculated when updating the first tab (user can do something else) and when the second tab is updated, it retrieves the saved results (when the input has not been changed).