簡體   English   中英

互斥提交

[英]Mutually exclusive submits

我有一個javascript,它在“ submit”事件上執行以下ajax調用(這又觸發了python腳本),我現在的問題是“如果有人在提交事件上單擊其他人單擊此ajax調用,應該通知提交中”。是否有人遇到此問題?(有名字嗎?),如何解決此問題? 請建議..

$("#main_form").submit(function(event) {
       .....................

            $.ajax({
                dataType: "json",
                type: "POST",
                contentType: "application/json",//note the contentType definition
                url: "scripts/cherrypick.py",
                data: JSON.stringify(data_cp),
                //data: data_cp,
                error : function (xhr, ajaxOptions, thrownError){
                    console.log("cherypick fail");
                    console.log(response);      
                    console.log(response['returnArray']);
                    alert(xhr.status);
                    alert(thrownError); 
                },
                success: function(response){
                    console.log("cherypick sucess");
                    console.log(response);
                    console.log(response['returnArray']);
                    var return_array = response['returnArray'];
                    console.log(return_array['faillist'].length);
                    console.log(return_array['picklist'].length);       
                    for (var i = 0; i < ip_gerrits.length; ) {
                        for (var j = 0; j < return_array['faillist'].length; ) {
                            if (ip_gerrits[i] != return_array['faillist'][j] )
                                ipgerrits_pickuplist.push(ip_gerrits[i]);
                            j++;
                        }
                        i++;
                    }

好的,就您要同步所有用戶的請求處理而言,應該在服務器端完成。 我假設您的服務器端是Python,即使您沒有在問題中添加相關標簽。 我的偏好是C#和PHP,但在您的情況下,我將執行以下操作...

選項#1-會話

1)為Python添加或安裝首選的會話模塊,人群建議使用Beaker

用於會話管理的Python模塊

2)發送AJAX請求到服務器端腳本

$(form).submit(function(e) {

    var options = {
         url: "scripts/cherrypick.py"
    };

    $.ajax(options);

});

3)此服務器端腳本將具有類似以下代碼的內容

session_opts = {
    'session.type': 'file',
    'session.data_dir': './session/',
    'session.auto': True,
}

app = beaker.middleware.SessionMiddleware(bottle.app(), session_opts)

@hook('before_request')
def setup_request():
    request.session = request.environ['beaker.session']

@route('/cherrypick')
def index():
    if 'processing' in request.session:
        data = { 'procesing': request.session['processing'] }
        return data

    processor()

def processor():

    request.session['processing'] = 1

    # Do some processing here for the first request
    # When processing is done you can clear "state" variable in session

    del request.session['processing']
    request.session.modified = True

4)現在,在JS腳本中,如果您獲取包含鍵“處理中”的JSON,則可能會向用戶顯示警告,他需要等待直到處理第一個請求

選項2-長輪詢和彗星

此選項的描述可能需要更多的空間來描述,因此最好看一下這篇文章,它有一個非常漂亮,干凈的示例以及在Python中進行長輪詢的實現

http://blog.oddbit.com/2013/11/23/long-polling-with-ja/

這里的主要思想不是保持靜態會話,而是使用無限循環,它可以根據某些狀態變量發送回不同的HTTP響應:

@route('/cherrypick')
def index():

    while True :

        response = { 'processing': processing }
        print response

        if processing != 1 :
            processing = 1
            # Do some processing
            processing = 0

        sleep(5)

最簡單的方法是關閉一個標志,該標志指示正在進行某些處理:

var processing = false;   
$("#main_form").submit(function(event) {
    if (processing) {
        $("#some_notification_pane").text("hold on there, turbo!");
        return;
    }
    processing = true;
    ...
    $.ajax({
        ...
        error: function(xhr, ajaxOptions, thrownError) {
            ...
            processing = false;
        },
        success: function(response) {
            ...
            processing = false;
        }
    });
    ...
});

您可能還想在提交處理程序(在其中我正在processing = true )的開頭禁用提交按鈕,並在收到響應后重新啟用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM