Google App Engine を使用して Web アプリケーションを作成しています。XML フィードから取得したライブの一時的な情報に基づいて、スクリプトでユーザー プロファイルを頻繁に更新したいと考えています。GAE background_thread を使用してこれを行っているため、これが実行されている間もサイトが動作し続けることができます。
このバックグラウンド スレッドの外でも、ユーザーは Web サイトをナビゲートして、プロファイルを変更できます。
バックグラウンド スレッドは、ライブ XML データに基づいてユーザー プロファイルを更新し、プロファイルをデータストアに再入力するという、本来の処理を正確に実行します。ただし、ユーザーがプロファイルを変更しても、バックグラウンド スレッドは変更を認識しません。ndb データストア クエリから返されたリストは、ユーザーが行った変更を反映していません。
興味深いのは、新しいユーザーがデータストアに追加された場合は正しい変更が反映されますが、既存のユーザー プロファイルが変更された場合は変更が反映されないということです。バックグラウンドスレッドからデータストアをクエリ/プットできるはずですか?
背景スレッドの要点:
def update_accounts():
while True:
# Get data from XML feed.
info_dict = get_live_data()
# Get all the users from the GAE database
gprofiles = mUserStats.query()
for profile in gprofiles:
# This isn't the actual condition but there's a condition here.
if needs_update in profile.m_needsUpdate:
# Modify the current profile.
profile.make_change(info_dict)
# Re enter into database.
profile.put()
# Add a sleep time as this doesn't need to run that frequently.
time.sleep(20)
クラス updateAccounts():
def start_thread(self):
t =background_thread.start_new_background_thread(target=update_accounts())
これは、プロファイルが変更される場所です。
def post(self):
session = get_current_session()
user_key = mUserStats_key(session['me'].m_email)
curr_user = mUserStats.get_by_id(session['me'].m_email, user_key)
curr_user.change_profile()
curr_user.put()