Various ajax calls with polling loop

Suppose the first ajax call is immediately executed, the loop function called by the controller 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 clicks 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 are talking about asynchronous stuff :)), but the text does not print until the FirstAjax loop is completely executed.

I want the second request to tell python to terminate from the first request, but I don’t know how to do this!

+1
source share
3 answers

Use Celery .

Here's the process:

  • FirstAjax. Python . //. FirstAjax , .

  • SecondAjax, . , . .

0

, Ajax , , . , Ajax , :

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

. .

0

, web2py.

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

Web2py conversations do not block session files, so a second ajax can begin immediately. Another way is to install:

session.connect(request, response, db)

in your models, this means that the session will not be saved in files, but in your DAL "db", so that the session will not be blocked.

These two solutions are the same for what I need.

In my case, I also need to release the device when the "Back" button is pressed, just added a flag that needs 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 to everyone and hope this helps!

0
source

All Articles