用户互相干扰实例

发布于 2024-10-19 02:38:21 字数 1587 浏览 2 评论 0原文

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

#views.py

def 显示(请求): 跳过的问题=[] 问题编号=[] user_answer_list=[] 答案列表=[] 所有问题=[] 如果 request.method=='POST': 初始值=1 id_列表=[] 结果=Questions.objects.all() 对于结果中的 i: 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)

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

In my multiple choice Quiz show project on google app engine multiple users can use the webapp simultaneously once they are login. But due to some reason they are interfering with each others instances.
Scenario example: Suppose user A wants to use the quiz show for 10 questions and at the same time user B wants to run the quiz show for 10 questions on another machine. But since they are using the application at the same, they are only getting 5 questions each and their result getting messed up.
Does anybody know how to avoid it ? I am not using any session or cookies till now. Is that a solution or something else?
Thanks

#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)

Followup-@Adam:Hi,I have removed global variables and again the program is working fine when I am working alone on my laptop. But when I am asking my colleague to try from his end,we both are getting same questions and interfering in each others sessions due to which end output getting messed up. I started using gae-sessions and able to use request.session but how should I use gae-sessions in this scenario.
Let me know if I am not clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拧巴小姐 2024-10-26 02:38:21

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

完全假设的示例代码:

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 代码上提供了许多优秀的实现。

以下是一些提供 cookie 支持的库可供选择。还有更多。

gae-utilities

gae-sessions

app-engine-oil

FOLLOWUP,基于您刚刚添加的示例代码:
我不想对此说得太细,但你所做的只是行不通。

使用全局变量通常是一个坏主意,但它特别是一个不可行的主意一段代码中的想法将被许多不同的用户以重叠的方式调用。我能给您的最好建议是采用所有令人痛苦的全局变量(它们实际上特定于特定用户),并将它们存储在特定于特定用户的字典中。我上面发布的 pickling/unpickling 代码是一种可行的方法,但说实话,除非您摆脱这些全局变量,否则您的代码将无法工作。

Without some concrete details about what kind of data your application stores to make one session different from any other, it is impossible to give you anything really useful, but one approach would be to store it in memcache keyed off of the user's user_id.

Completely hypothetical for-example code:

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)

Now, before you go cutting and pasting, there are a lot of caveats to this approach, and it is meant only as a suggested starting point. Memcache is not guaranteed to hold items in memory, and there are plenty of other competing implementations that would be more reliable in some respects.

Fundamentally, I'd suggest using cookies to store the session data, but AppEngine doesn't have native support for cookies, so you'd have to go find an implementation of them and include it in your code. There are a number of fine implementations that are available on Google Code.

Here are some libraries to pick from that provide cookie support. There are even more.

gae-utilities

gae-sessions

app-engine-oil

FOLLOWUP, based on the sample code that you just added:
I don't want to put too fine of a point on it, but what you're doing just ain't gonna work.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文