简体   繁体   中英

WebWorker not concurrent with 2 XMLHttpRequest to php page with session_start();

On one page i have several DIVs that get filled with data from XMLHttpRequests to different php pages. To keep the UI responsive i started experimenting with webworkers. It seemed however that a faster page kept waiting after a slower page, ie the web worker didn't work concurrently. I've simplified the pages for testing purposes, see code below. It seems that when the 2 back end php pages that provide the data have if (!isset($_SESSION)) session_start(); in them, one page is qeued after the other. In the example below, there are 2 buttons that each invoke a different web worker that in turn invokes a different php script. PhpA is 10sec slower than phpB. So when you click on button wwA and then on wwB in the main (test.php) script, you should first get a response from phpB. When if (!isset($_SESSION)) session_start(); is in phpA and in phpB this is not the case. Why is this so?

test.php

<?php if (!isset($_SESSION)) session_start();?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
<title>test</title>
<script language="JavaScript" type="text/javascript">
<!--
function launchWebWorkerA() {
    var worker = new Worker('webWorkerA.js');
    worker.addEventListener('message', function(e) {
        document.getElementById('outputA').innerHTML = e.data.text;
        worker.terminate();
    }, false);
    worker.postMessage();
}
function launchWebWorkerB() {
    var worker = new Worker('webWorkerB.js');
    worker.addEventListener('message', function(e) {
        document.getElementById('outputB').innerHTML = e.data.text;
        worker.terminate();
    }, false);
    worker.postMessage();
}
//-->
</script>
</head>
<body>
<input type="button" onClick="launchWebWorkerA()" value="wwA" />
<input type="button" onClick="launchWebWorkerB()" value="wwB" />
<div id="outputA">outputA</div>
<div id="outputB">outputB</div>
</body>
</html>

webWorkerA.js

// JavaScript Document
self.addEventListener('message', function(e) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
            self.postMessage({'text': xmlhttp.responseText});
        }
    }
    xmlhttp.open('POST','phpA.php',true);
    xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    xmlhttp.send();
}, false);

webWorkerB.js

// JavaScript Document
self.addEventListener('message', function(e) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
            self.postMessage({'text': xmlhttp.responseText});
        }
    }
    xmlhttp.open('POST','phpB.php',true);
    xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    xmlhttp.send();
}, false);

phpA.php

<?php if (!isset($_SESSION)) session_start();
    sleep(10);
    echo 'phpA response3';
?>

phpB.php

<?php if (!isset($_SESSION)) session_start();
    echo 'phpB response3';
?>

Starting a session will block all other scripts attempting to start the exact same session (based on the cookie). This is one reason why you need to minimize the amount of time a session is opened for. Arguably you could use a database or something like memcache to kludge inter-script communication but it's not really what PHP is about.

Source: ajax multi-threaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM