1

Let's assume the first ajax call is immediately made, the controller-called function loops until something is read, for example:

def FirstAjax():
    while True:
        if something is read:
            val = something
            break
    return val

Before something is read the user presses the "Back" button and a new ajax request is sent, for example:

def SecondAjax():
    print "Response from second ajax"

The second ajax call is really called (obviously, we're talking about async stuff :) ) but the text is not printed until the FirstAjax loop is completely done.

I want the second request to tell python to stop the action from the first request but don't know how it could be done!

1

3 Answers 3

0

Use Celery.

Here's the process:

  1. Make FirstAjax request. Python queues the task using celery. You can have the task start immediately or in a few minutes/hours/days. FirstAjax sends back the id of the task created, while the task itself is queued to be executed in the background. Using celery task ids

  2. Make SecondAjax, sending along the task id. Use that task id to cancel the task. How to cancel a celery task.

0

It's possible that the second Ajax request is being blocked until the first completes because the session file may be locked. Assuming the first Ajax request doesn't need to use the session, you can have it unlock the session:

def FirstAjax():
    session.forget(response) # unlock the session file
    [rest of code]

See here for more details.

0
0

Problem resolved, it was a specific web2py problem.

def FirstAjax():
    session.forget(response) # unlock the session file
    [rest of code]

Talks web2py to don't lock session files, so that second ajax can start immediately. An other way is to set:

session.connect(request, response, db)

in your models, it means session will not be saved in files but in your DAL "db", so that session will not be locked.

These two solutions are the same for what I need.

In my case I also need to do a device release when the back button is pressed, just added a flag to be checked in the polling cycle, for example:

def FirstAjax():
    session.forget(response) # unlock the session file
    HSCAN.SetLeave(False)
    HSCAN.PollingCycle()
    #Rest of code    

def SecondAjax():
    HSCAN.SetLeave(True)
    #Rest of code 

class myHandScanner():
    def __init__(self):
        self.leave = False

    def SetLeave(self, leave):
        self.leave = leave

    def PollingCycle(self):
        while True:
            if self.leave:
                #Do device release
                return
            if something is read:
                val = something
                break
        #Do device release
        return val

Thanks everybody and hope this helps!

1
  • If you need session in your ajax calls, use second way (session.connect(request, response, db) ), otherwise you'll not be able to get the session values you have saved in the action! May 10, 2012 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.