PHP脚本的多个实例不会在同一个浏览器中从同一个URL同时加载

编程入门 行业动态 更新时间:2024-10-24 04:52:24
本文介绍了PHP脚本的多个实例不会在同一个浏览器中从同一个URL同时加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图让php脚本的两个实例同时运行。我有一个脚本'test.php':

< p><?php echo time(); ?>睡觉...< / p为H. <?php sleep(5); ?> < p><?php echo time(); ?>完成< / p为H.

如果我同时在两个浏览器选项卡中加载页面,我可以得到:

1446855680睡觉... 1446855685完成

和这个: $ b

1446855686睡觉...

1446855691完成

其中一个实例阻塞,直到其他加载。这在Firefox和Chromium上都会发生。

如果我制作第二个相同的脚本test2.php,或者将两个url重写为相同的脚本,

1446855862睡觉...

1446855867完成

以及:

1446855863睡觉... 1446855868完成

两个实例都在加载与此同时。因此,这是相同的URL被阻止。

如何获得具有相同URL的脚本的两个实例同时加载/运行?

解决方案

正如VolkerK指出的那样,您的测试证实Firefox / Chrome按顺序对完全相同的 URL执行请求。但实际上这不是问题,因为用户通常不会加载同一页面两次,而对于向Ajax请求提供数据的脚本,由于不同的参数被附加,所以URL会有所不同。

如果你想绕过这个机制,你可以在URL中附加一个随机参数(在不同的浏览器标签中有所不同),即 url.to/your/script?urlID = 4821de7e524cf762deab6ed731343466 。随机参数使浏览器看到两个不同的URL,从而并行执行加载。

I'm trying to get two instances of a php script to run concurrently. I have a script, 'test.php':

<p><?php echo time(); ?> Sleeping...</p> <?php sleep(5); ?> <p><?php echo time(); ?> done</p>

If I load the page in two browser tabs at the same time, I get this:

1446855680 Sleeping...

1446855685 done

and this:

1446855686 Sleeping...

1446855691 done

One instance blocks until the other loads. This happens on both Firefox and Chromium.

If I make a second identical script, 'test2.php', or rewrite two urls to the same script, and load the two pages in different tabs, I get this:

1446855862 Sleeping...

1446855867 done

and this:

1446855863 Sleeping...

1446855868 done

Both instances are loading at the same time. So it is identical URLs that are being blocked.

How can I get two instances of a script with the same URL to load/run at the same time?

解决方案

As VolkerK pointed out and your testing confirmed Firefox/Chrome perform requests for the exact same URL in sequence. However in practice this is not a problem since Users usually do not load the same page twice, and for a script that serves data to Ajax requests the URLs will differ since different parameters are appended.

Should you want to bypass this mechanism in general you can append a random parameter (has to be different in the different browser tabs) to the url, i.e. url.to/your/script?urlID=4821de7e524cf762deab6ed731343466. The random parameter causes the browser to see two different URLs and thus to perform the load in parallel.

更多推荐

PHP脚本的多个实例不会在同一个浏览器中从同一个URL同时加载

本文发布于:2023-10-30 04:07:18,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   脚本   实例   器中   加载

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!