-1

在我的谷歌应用程序引擎上的多项选择测验显示项目中,多个用户一旦登录就可以同时使用网络应用程序。但是由于某种原因,它们会干扰彼此的实例。场景示例:假设用户 A 想要使用 10 个问题的问答节目,同时用户 B 想要在另一台机器上运行 10 个问题的问答节目。但是由于他们同时使用该应用程序,所以他们每个人只得到 5 个问题,而且他们的结果变得一团糟。有人知道如何避免吗?到目前为止,我没有使用任何会话或 cookie。这是解决方案还是其他?谢谢

#views.py

def display(request): skipped_questions=[] question_number=[] user_answer_list=[] answer_list=[] all_questions=[] if request.method=='POST': initial_value=1 id_list=[] result=Questions.objects.all () for i in result: id_value=i.id id_list.append(id_value)

    data=request.POST.copy()
    total_question=data['number_of_question']
    mytime=data['time']
    seconds=59
    minutes=int(mytime)-1
    already_questions=Random_model.objects.all().delete()
    already_answers=User_answer.objects.all().delete()
    random_questions_list=random.sample(id_list,int(total_question))
    for i in random_questions_list:
        random_model=Random_model()
        random_model.list_id=i
        random_model.initial_value=int(initial_value)
        random_model.save()
        initial_value+=1
    question_list=1
    a=Random_model.objects.get(initial_value=question_list)
    new_question=Questions.objects.get(id=a.list_id)
    template_value={ 'output': new_question,'minutes':minutes,'seconds':seconds,'question_list':question_list }
    return render_to_response("quiz.html",template_value)

跟进-@Adam:嗨,我已经删除了全局变量,当我独自在笔记本电脑上工作时,程序再次运行良好。但是,当我要求我的同事从他的一端尝试时,我们都遇到了相同的问题并干扰了彼此的会话,因为最终输出变得混乱。我开始使用 gae-sessions 并且能够使用 request.session 但在这种情况下我应该如何使用 gae-sessions。如果我不清楚,请告诉我。

4

1 回答 1

2

如果没有关于您的应用程序存储什么样的数据以使一个会话与任何其他会话不同的一些具体细节,就不可能为您提供任何真正有用的东西,但一种方法是将其存储在与用户的 user_id 键控的 memcache 中。

完全假设的示例代码:

def get_session_data():
    from google.appengine.api import users

    found_session = None

    user = users.get_current_user()
    if user:
        from google.appengine.api import memcache

        users_session = memcache.get(user.user_id())


    return found_session

def save_session_data(session_object):
    from google.appengine.api import users
    from google.appengine.api import memcache

    memcache.set(users.get_current_user().user_id(), serialized_object)

现在,在你开始剪切和粘贴之前,这种方法有很多警告,它只是作为一个建议的起点。Memcache 不能保证将项目保存在内存中,并且还有许多其他竞争实现在某些方面会更可靠。

从根本上说,我建议使用 cookie 来存储会话数据,但 AppEngine 没有对 cookie 的本机支持,因此您必须找到它们的实现并将其包含在您的代码中。Google Code 上有许多很好的实现。

这里有一些提供 cookie 支持的库可供选择。还有更多。

gae 实用程序

会议

应用引擎油

跟进,基于您刚刚添加的示例代码:我不想对它说得太细,但是您正在做的只是行不通。

Using global variables is generally a bad idea, but it is specifically an unworkable idea in a piece of code that is going to be called by many different users in an overlapping-fashion. The best advice that I can give you is to take all of the painful global variables (which are really specific to a particular user), and store them in a dictionary that is specific to a particular user. The pickling/unpickling code that I posted above is a workable approach, but seriously, until you get rid of those globals, your code isn't going to work.

于 2011-02-24T18:45:08.330 回答