【问题标题】:Why are my XHR calls waiting for each other to return a response为什么我的 XHR 呼叫要等待对方返回响应
【发布时间】:2015-01-21 23:46:10
【问题描述】:

我在页面中有一个 iframe,它不断地轮询服务器以获取由“主”XHR 主动更新的会话变量。

所以基本上:

  1. Main XHR 运行并执行其操作,在运行时更新会话变量。通常需要一段时间,比如说超过 10 秒。

  2. 当主 XHR 运行时,我使用并行 XHR 请求轮询服务器以获取相同的会话变量。每当我收到来自我的投票 XHR 的响应时,我都应该更新前端视图。

问题是轮询 XHR 直到主 XHR 完成后才返回任何内容,此时它们当然已经无用。这真的是处理会话时的预期行为吗?类似于每个客户端连接一个会话的限制?

编辑:

这里有一些代码 sn-p。代码非常大,所以我试着把它精简到最基本的部分。因为我刚刚从源代码中取出了重要部分,所以这里输入的可能有一些语法错误。

生成 iframe

(function($) {
    $(document).on('click','#proceed_form',function(){
        $('#upload_frame').show(); 
        function set () { 
            $('#upload_frame').attr('src','/productUpload/generateIframe'); 
        }
        setTimeout(set); 
    });
});

内嵌框架

<script type='text/javascript' src="/assets/js/src/vendor/jquery-1.9.1.js" ></script>

<script>

(function($) {

    $(document).ready(function() { 
        setInterval(function() 
        {
            $.get("/productController/getProgress", function(data)
            {
                $('#progress_container').fadeIn(100);   //fade in progress bar  
                $('#progress_bar').width(data +"%");    //set width of progress bar based on the $status value (set at the top of this page)
                $('#progress_completed').html(parseInt(data) +"%"); //display the % completed within the progress bar
            }
        )},500);  

    });

})(jQuery);

</script>


<div id="progress_container">
    <div id="progress_bar">
         <div id="progress_completed"></div>
    </div>
</div>

PHP 应用程序

class productUpload extends CI_Controller{ 

    /**
     * Respond to XHR poll request
     *
     */
    public function getUploadedBytesToCloud()
    {
        session_start();
        $uploadedBytes = $_SESSION['bytes_uploaded'];
        echo json_encode(['uploadedBytes' => $uploadedBytes]);
    }

    /**
     * Main controller action
     * Uploads the images of a product to the cloud
     *
     */ 
     public function moveProductImagesToCloud($productId)
     {
          /**
           * Some logic to get the product image directory
           *
           */
           $productPath = '/assets/product/image_dir';
           $directoryMap = directory_map($productPath);
           foreach($directoryMap as $key => $file){
                 /**
                  * Upload file to AWS S3 bucket
                  */ 
                 $this->awsUploader->uploadFile(...);

                 $fileSize = $_SESSION['bytes_uploaded'];
                 $fileSize += filesize(getcwd()."/".$productPath."/".$file);
                 $_SESSION['bytes_uploaded'] = fileSize;
            }
      }

}

【问题讨论】:

  • 请提供您用于制作和发送 XHR 的实际代码 - 从您的描述中无法判断发生了什么。
  • 如果您尝试在两个异步调用中更新和获取相同的变量,听起来您会遇到某种竞争条件。
  • @Sacho 我添加了一些代码 sn-ps。
  • @Vidur 可能是这种情况,但我无法判断,因为所有响应都同时返回。
  • @IntegralWind-up 您可能可以通过将时间戳记录到控制台来辨别它们返回的顺序 - 尽管根据竞争条件的定义,它们可能会以未定义的顺序返回。代码 sn -p 以毫秒为单位获取时间戳:(new Date()).getTime()

标签: javascript php ajax apache session


【解决方案1】:

是的,默认会话管理器(使用文件)在您执行 session_start 时锁定会话文件,并在您执行 session_write_close(或脚本结束)时释放它。同时其他脚本试图访问会话,等待发布。详细文章here或手册session-write-close

【讨论】:

  • 我以为是这样。感谢您的确认。我正在尝试使用框架的会话实现而不是 PHP 的本机会话,但没有成功。但至少我可以继续用头撞另一面墙。谢谢。
猜你喜欢
  • 2019-12-11
  • 1970-01-01
  • 2018-08-08
  • 2022-01-01
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多