示例#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'),
    ]
示例#2
0
    def run(self):
        from common import cache

        process_pool[self.ident] = self

        self.last_results = None
        print('%s: run process' % self.uuid)

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

            if cache.get('thread-%s-kill' % self.uuid):
                return False
            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('thread-%s-finished' % self.uuid, True)
        print('%s: process finished' % self.uuid)

        del process_pool[self.ident]
示例#3
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]