与轮询周期不同的Ajax调用周期、不同、Ajax

2023-09-10 21:09:09 作者:木耳收割机

让我们假设第一个Ajax调用立即进行,控制器调用的函数循环,直到一些被读取,例如:

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

在一些被读取用户presses的后退按钮和一个新的Ajax请求被发送,例如:

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"

第二AJAX调用真的叫(很明显,我们在谈论异步的东西:)),但文字不打印,直到FirstAjax回路是完全做得到。

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.

我想第二个请求告诉Python阻止第一次请求的动作,但不知道如何可以做到!

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!

推荐答案

问题解决了,这是一个特殊的web2py问题。

Problem resolved, it was a specific web2py problem.

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

会谈的web2py以不锁的会话文件,从而使第二AJAX可以立即开始。 一个另一种方法是设置:

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)

在你的模型,这意味着会议不会保存在文件中,但在你的DALDB,以使会话不会被锁定。

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.

在我来说,我也需要做设备释放时,后退按钮是pressed,只是增加了一个标志轮询周期进行检查,例如:

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!

 
精彩推荐
图片推荐