开发者

Users interfering with each others instances

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 somet开发者_如何学JAVAhing 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜