How to trigger an ajax call at the same time - php

It might be a stupid question, but I can't easily understand why it behaves this way

I have two ajax call functions,

execCommand (): to execute shell command via php

shell_exec ('ping 127.0.0.1> ../var/log/tmp.log');

getLog (): to collect execCommand () logs [../var/log/tmp.log] and display to user [live read]

echo file_get_contents (../var/log/tmp.log);

This is a jquery ajax script

    $("#docker_install_fek").on("click",function(){

        $('#progress-window').show();
        getLog();
        execCommand();
    });

    function execCommand(){

        $.ajax({
            url: '<?php echo $block->getUrl('meta/reports/ajax',[]);?>',
            type: 'POST',
            data:{ip:$("#ip").val()},
            success: function(data) {
                alert(data);
            },
            error: function() {
                alert("fail");
            }
        });

    }
    function getLog() {

        $.ajax({
            type: "GET",
            url: "<?php echo $block->getUrl('meta/reports/progress',[]);?>",
            dataType: "text",
            success: function (data) {
                $("#progress-window").empty();
                $("#progress-window").append("<pre>"+data+"</pre>");
                $("#progress-window").scrollTop(1E10);
                setTimeout(getLog, 3000);
            },
            headers: {
                "Range" : "bytes=-500"
            }
        });
    }

      

I can see in the netbug net tab, both ajax calls are loaded but never log out until execCommand () finishes executing.

I don't know what the main reason is, I am testing how to run ping 127.0.0.1 > ../var/log/tmp.log

from terminal and then comment execCommand () now getLog (), update log file updates every 3 seconds.

+3


source to share


1 answer


You are using sessions. On the server side, sessions are locked, so you can only open session files one script at a time.



If you have server-side scripts that need to be non-blocking, then either don't use session_start () in them, or remember to close the session with session_write_close () as soon as you can to allow other scripts to run.

0


source







All Articles