Exemplo n.º 1
0
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'),
    ]
Exemplo n.º 2
0
    def run(self):
        from common import cache

        process_pool[self.ident] = self

        func_hash = generate_cache_key(simulate_individuals, var_store=self.variables)

        self.last_results = None
        print('%s: run process (func hash %s)' % (self.uuid, func_hash))

        def step_callback(df, force=False):
            now = time.time()
            if force or self.last_results is None or now - self.last_results > 0.5:
                print('%s: set results to %s' % (self.uuid, func_hash))
                cache.set('%s-results' % func_hash, df, timeout=30)
                self.last_results = now

            return True

        try:
            df = simulate_individuals(step_callback=step_callback, variable_store=self.variables)
        except ExecutionInterrupted:
            print('%s: process cancelled' % self.uuid)
        else:
            print('%s: computation finished' % self.uuid)
            step_callback(df, force=True)

        cache.set('%s-finished' % func_hash, True)
        print('%s: process finished' % self.uuid)

        del process_pool[self.ident]
Exemplo n.º 3
0
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]
Exemplo n.º 4
0
 def __init__(self, variables):
     self.variables = variables
     super().__init__(daemon=True)
     self.uuid = str(uuid.uuid4())
     self.cache_key = generate_cache_key(simulate_individuals, var_store=self.variables)
     self.cache_expiration = 30