2
グローバル変数を使用するのは良いコーディング手法ではありません。スタートアップ時にマシン学習モデルをロードする必要があります。そのため、リクエストごとに処理する必要はありません。私は要求を処理するためにFlaskを持っています。しかし、私はPythonで起動時に変数を初期化するのがよい習慣であるか理解できません。 - あなたは上のモデルをロードする場合を除きグローバル変数の回避とPythonでの起動時のモデルのロード
Class A{
private ClassB classB;
A() {
//Load some file into memory
classB = new ClassB();
classB.x = //set from file
}
public static getClassB() {
return classB;
}
}
Is this something which is a good practice to follow in Python as well? I could then probably do something like
@app.route('/', methods=['GET'])
def main():
b = getClassB() ; //this time it shouldn't load
score = b.predict()
return score
if __name__ == 'app':
try:
getClassB() //this will load once at startup
except Exception,e :
print 'some error'
'before_first_request'にモデルをロードして' g 'に格納するのはどうですか? – stamaimer
@stamaimerここを参照してくださいhttps://stackoverflow.com/questions/15083967/when-should-flask-g-be-used - 私はあなたがどのように考えるかg働いているとは思わない – cal97g