def run_simulation_callback(n_clicks, simulation_days): from flask import session from common import cache print('run simulation (days %d)' % simulation_days) set_variable('simulation_days', simulation_days) if n_clicks: set_variable('random_seed', n_clicks) df = simulate_individuals(only_if_in_cache=True) if df is not None: return render_results(df) existing_thread_id = session.get('thread_id', None) if existing_thread_id: cache.set('thread-%s-kill' % existing_thread_id, True) process = SimulationThread(variables=session.copy()) session['thread_id'] = process.uuid process.start() return [ dcc.Interval(id='simulation-output-interval', interval=500, max_intervals=60), html.Div(id='simulation-output-results'), ]
def run_simulation_callback(n_clicks, simulation_days): print('run simulation (days %d)' % simulation_days) print(generate_cache_key(simulate_individuals)) set_variable('simulation_days', simulation_days) if n_clicks: set_variable('random_seed', n_clicks) df = simulate_individuals(only_if_in_cache=True) if df is not None: return render_results(df) if settings.RESTRICT_TO_PRESET_SCENARIOS: return [html.Div('Palvelussa ruuhkaa, osa simulaatiotoiminnallisuuksista on pois käytöstä')] existing_thread_id = session.get('thread_id', None) if existing_thread_id: cache.set('thread-%s-kill' % existing_thread_id, True) process = SimulationThread(variables=session.copy()) session['thread_id'] = process.uuid process.start() return [ dcc.Interval(id='simulation-output-interval', interval=500, max_intervals=60), html.Div(id='simulation-output-results'), ]
def update_simulation_results(n_intervals): thread_id = session.get('thread_id', None) if thread_id is None: raise dash.exceptions.PreventUpdate() func_hash = generate_cache_key(simulate_individuals) cache_key = '%s-results' % func_hash df = cache.get(cache_key) if df is None: print('%s: no results' % func_hash) raise dash.exceptions.PreventUpdate() if cache.get('%s-finished' % func_hash): # When the computation thread is finished, stop polling. print('thread finished, disabling') disabled = True else: print('thread not finished, updating') disabled = False out = render_results(df) return [out, disabled]
def update_simulation_results(n_intervals): from flask import session thread_id = session.get('thread_id', None) if thread_id is None: raise dash.exceptions.PreventUpdate() df = cache.get('thread-%s-results' % thread_id) if df is None: raise dash.exceptions.PreventUpdate() if cache.get('thread-%s-finished' % thread_id): # When the computation thread is finished, stop polling. print('thread finished, disabling') disabled = True interval = 5000 else: print('thread not finished, updating') disabled = False interval = 500 out = render_results(df) return [out, disabled, interval]