长时间运行的回调 TAIPY

问题描述 投票:0回答:1

如果回调长时间运行。我已经实现了以下代码。

def start_analytics(project_id,instance_id):
    missing_signals = []
    signal_dict = get_all_timeseriesdata(project_id,instance_id)
    print("got dict")
    print(signal_dict)
    
    # Check if 'X', 'Y', and 'Z' keys exist in the dictionary
    if 'X' not in signal_dict:
        missing_signals.append('X')

    if 'Y' not in signal_dict:
        missing_signals.append('Y')

    if 'Z' not in signal_dict:
        missing_signals.append('Z')

    if missing_signals:
        print("signals missing")
        #notify(state, "info", f"The following signals are missing: {', '.join(missing_signals)}", True)
    else:
        print("checking inxed of x,y,z")
        x_id = signal_dict.get('X')
        y_id = signal_dict.get('Y')
        z_id = signal_dict.get('Z')

        print(project_id,instance_id,x_id)
        x_df = read_timeseriesdata(project_id,instance_id,x_id)
        y_df = read_timeseriesdata(project_id,instance_id,y_id)
        z_df = read_timeseriesdata(project_id,instance_id,z_id)
        print("got all df")

        # Merge the DataFrames along the columns axis (axis=1)
        merged_df = pd.concat([x_df, y_df, z_df], axis=1)
        selected_df = merged_df[['X', 'Y', 'Z']]
        print(selected_df.shape[0])
        print(selected_df.head())
        total_points_rsi = selected_df.shape[0]
        print("total rsi points",total_points_rsi)
        
        #state.total_points_rsi = total_points_rsi

        # Using Plotly for an interactive 3D plot
        fig = go.Figure(data=[go.Scatter3d(
            x=selected_df['X'],  # X-axis values
            y=selected_df['Y'],  # Y-axis values
            z=selected_df['Z'],  # Z-axis values
            mode='lines',
            line=dict(width=1, color='red'),
            name='RSI Data',
            hovertext=[f"Index of RSI point: {index}" for index in merged_df.index]
        )])

        files = get_all_files(project_id,instance_id)
        #print(files)
        src_df = read_src_data(project_id,instance_id,files)
        #print(src_df)

        print("got src file")

        # Apply the specific linear interpolation to the SRC path
        df_src_interpolated_specific = linear_interpolate_path_specific_points(src_df, total_points_rsi)

        # # Add interpolated SRC data as markers to the plot
        fig.add_trace(go.Scatter3d(
            x=df_src_interpolated_specific['X'],
            y=df_src_interpolated_specific['Y'],
            z=df_src_interpolated_specific['Z'],
            mode='markers',  # Plot as individual markers
            marker=dict(size=2, color='green'),  # Adjust size and color as needed
            name='Interpolated SRC Path',
            hovertext=[f'Index: {i}' for i in df_src_interpolated_specific.index]  # Custom hover text with index
        ))
        return True

def heavy_function_status(state, status):
    if status:
        notify(state, "success", "The heavy function has finished!")
    else:
        notify(state, "error", "The heavy function has failed")

def plot_path(state):
        notify(state, "info", "Gathering resourses started",duration=3000)
        project_id = state.form_visualize.v_selected_projects.id
        instance_id = state.form_visualize.v_selected_instance.id
        invoke_long_callback(state, start_analytics,[project_id,instance_id],heavy_function_status,None,period=0)
        notify(state, "info", "REST STARTED",duration=3000)

我注意到我们不应该在重函数中传递“状态”作为参数。但就我而言,我需要更新重函数内的绘图。我怎样才能实现这个?或者如果我需要从繁重的函数返回数据帧和其他资源,然后执行另一个 def。我该如何排序?

在这种情况下,我需要从重函数返回 3 df 并将其置于重函数状态以更新图,我如何返回 3 df?当我尝试返回时,我收到返回 df 的错误“bool object”

taipy
1个回答
0
投票

来自 Taipy 的 Alex 这里!

您可以使用状态函数定期更新状态变量。看看这个例子:

def long_forecast():
    time.sleep(60)
    return "Forecasting completed."


counter = 0


def long_forecast_status(state, status):
    if isinstance(status, bool):
        if status:
            notify(state, "success", f"Forecasting completed.")
        else:
            notify(state, "error", f"Forecasting failed.")
    else:
        state.counter += 1
        state.logs = f"Forecasting... ({state.counter}s)"


def predict(state):
    notify(state, "info", "Predicting... (this may take a while)")
    state.logs = "Forecasting started."
    invoke_long_callback(state, long_forecast, [], long_forecast_status, [], 1000)
© www.soinside.com 2019 - 2024. All rights reserved.