LIGHTGBM 泡菜输出不适用于多个 SANIC 工人(> 1),但与单个工人一起工作

LIGHTGBM pickle output not working with multiple SANIC workers (>1) but working with single worker

我正在尝试使用 Sanic 加载机器学习模型输出。我已经在 main 方法(全局定义)中加载了输出。当我将 sanic worker 设置为 1 但在全局定义时不与多个 sanic worker 一起工作时,它工作正常。我的代码无限期地等待模型生成所需的结果。

Its works when I load model output inside the function (e.g. here in the method modelrun) even if sanic workers >= 1

It works when I load model output globally(outside the function) but only if sanic workers = 1

It doesnot work when I load model output globally(outside the function) if sanic workers > 1

import pickle
import sanic

if __name__ == '__main__':
    df = pd.DataFrame()

    p_file_path = "/Users/pratiksha/FModel_06Jan_Smote_Sel_Vars_48.dat"
    pickle_file = open(path, 'rb')
    lbg_model_smote_sel_vars = pickle.load(pickle_file)

    modelrun(df, lbg_model_smote_sel_vars)

    app.run(host=app_host, port=int(app_port), debug=True,
            auto_reload=True, workers=int(10))


def modelrun(df_f, lbg_model_smote_sel_vars):
    training_pred_smote = lbg_model_smote_sel_vars.predict_proba(df_f)
    return training_pred_smote

使用的版本

  1. sanic==20.12.1

  2. lightgbm==3.3.1

  3. numpy==1.20.1

  4. pandas==1.2.4

  5. scikitlearn==1.0.2

  6. scipy==1.6.2

将 Sanic 版本升级到 21.12.1

当你有多个worker时,Sanic会启动一个主进程管理多个子进程。这些子进程将成为应用程序服务器工作者。

在此处查看循环。 https://sanicframework.org/en/guide/basics/listeners.html

加载pickle后,添加到context中似乎可以解决属性。

例如

# or main_process_start
@app.listener("before_server_start")
async def startup(app, loop):
    loaded = pickle.load(your_file)
    app.ctx.pickle = loaded

@app.get("/")
async def handler(request):
    # Call pickle
    request.app.ctx.pickle