问题描述
我试图让php脚本的两个实例同时运行。我有一个脚本'test.php':
< p><?php echo time(); ?>睡觉...< / p为H.
< p><?php echo time(); ?>完成< / p为H.
如果我同时在两个浏览器选项卡中加载页面,我可以得到:
和这个:
$ b
其中一个实例阻塞,直到其他加载。这在Firefox和Chromium上都会发生。
如果我制作第二个相同的脚本test2.php,或者将两个url重写为相同的脚本,
以及:
两个实例都在加载与此同时。因此,这是相同的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:
and this:
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:
and this:
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同时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!