私は、pythonanywhere.comでホストされているFlaskを使用してWeb上にチャットボットアプリケーションを構築していますが、同時に複数の人がボットをチャットすると、他の人とボットは最新の質問の文脈で答えます。クライアント間のPython Flaskセッションデータが分離されていません
Flaskでセッションを使用して関連するデータを分離しようとしましたが、同じ問題が発生しています。私はドキュメントを読んで、多くの例をユーザ名や電子メールで見てきましたが、私の場合、セッションIDをランダムに生成して、そのユーザの関連データすべてをインスタンスに関連付けるだけです。
は、私は基本的な理解に一般文書から使用状況やother questions を持ってthis question から秘密鍵を必要と知っていると私はそれがこれらのタイプのアプリケーションのために離れて、グローバル変数から滞在するのがベストです知っfrom this question
私は、各ブラウザのリクエストがセッションとそのデータを自動的に分離すると思っていましたが、何かが欠落しているはずです。私はここにコードの主要部分を掲載しました。問題の説明が不明な場合は、youngblksocrates.pythonanywhere.comの問題を、さまざまなブラウザのボットとチャットすることで確認できます。本当にありがとう!お時間&ため
from flask import Flask, request, url_for, render_template, session
import random
from user_info_object import UserInfo
from script_freeStyle import FreeStyleXXXX
import simplejson as json
app = Flask(__name__)
app.secret_key = "my secret key"
currentProfile = UserInfo() #helps bot know what the user has asked
learnBoutClass = FreeStyleXXXX() #conversation script to follow
greetings = ["HI","HEY","GREETINGS","HELLO","WASSUP", "WHAT UP"]
def preprocess(textblob):
return str(textblob.correct())
def bot_reply_to_this(input,scriptobj):
if input.upper() in greetings:
reply = random.choice(["Hello!", "Hi", "Hey", "Greetings", "*Waves*","What's up?"])
else:
currentProfile = UserInfo()
myspecificprofile = currentProfile.populate(session['profilestate'])
responseAndProfile = scriptobj.determineReply(myspecificprofile,input)
response = responseAndProfile[0]
updatedprofile = responseAndProfile[1]
session['lastrequestedinfo'] = scriptobj.lastRequestedInfo
session['profilestate'] = json.dumps(updatedprofile.__dict__)
return response
@app.route('/')
def user_chat_begins_fresh():
sessionID = ''.join(random.choice('ABCDEF') for i in range(16))
session.pop('ID',None)
session['ID'] = sessionID
session['lastrequestedinfo'] = ""
#everything gotta start fresh
takeClassScript.lastRequestedInfo = ""
learnBoutClass.lastRequestedInfo = ""
#create a new profile so that the state resets
currentProfile = UserInfo()
session['profilestate'] = json.dumps(currentProfile.__dict__)
del chathistory [:]
return render_template('init.html',urlToConversation=url_for('conversation_container'),inputVarName="input")
@app.route('/reply', methods=['POST'])
def conversation_container():
rawinput = request.form["input"]
session['input'] = rawinput
blob_input = TextBlob(session['input'])
cleaned_input = session['input']
chosenscript = learnBoutClass
session['lastrequestedinfo'] = chosenscript.lastRequestedInfo
session['reply'] = bot_reply_to_this(session['input'],chosenscript)
chathistory.append("You: " + session['input'] + "\n")
chathistory.append("Bot: " + session['reply'] + "\n")
printedhistory = "\n".join(chathistory)
session['history'] = printedhistory
return render_template('conversation.html',\
output=session['history'] ,\
urlToConversation=url_for('conversation_container'),\
inputVarName="input",\
urlToFreshChat=url_for('user_chat_begins_fresh'))
おかげで冗長な質問には申し訳ありません!
私は実際には、すべてのリクエストでトップレベル(モジュールレベル)の変数が**上書きされないことを意味すると思います。そのため、両方のブラウザが同じcurrentProfileを使用しているため、チャットが混在しています。 – conrad
それはどんなクライアントからのリクエストでも上書きされてしまうので、チャットは混ざっていますよね?ブラウザ#1と同じように、currentProfileは "foo"という情報を持ち、ブラウザ#2は何かを発し、currentProfileはブラウザ#1とブラウザ#2の "bar"になります。だから、ブラウザ#1が新しい質問をするとき、currentProfileはまだ "bar"であり、これはユーザが予想するのとは違ってボットが質問に答えるようにする。 –
多分私は "要求"を間違って使用しています。私はWeb開発の経験が限られています。私たちも同じことを言っているようです。モジュールレベルの変数がクライアント間で共有**されていると言う方が正確でしょうか? @conrad –