对 PHP 脚本的同时请求
如果 PHP 引擎已经在服务器上执行脚本,其他浏览器同时请求同一脚本会发生什么情况?
- 请求会排队吗?
- 他们会被忽视吗?
- 每个请求都有自己的脚本吗 实例?
- 还有其他可能吗?
If the PHP Engine is already in the middle of executing a script on the server what would happen to other simultaneous browser requests to the same script?
- Will the requests be queued?
- Will they be ignored?
- Will each request have its own script
instance? - Any other possibility?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
服务器根据其配置,通常可以同时处理数百个请求——如果使用 Apache,
MaxClients
配置选项是这样说的:两个客户端请求同一页面这一事实并不是问题。
所以 :
不 ;除非:
MaxClients
- 请参阅前面 Apache 手册中的引用。否:这意味着只有一个用户可以同时使用一个网站;这不太好,不是吗?
如果是这样的话,如果您同时按 F5 看看是否有人回答,我就无法发布此答案!
(PHP中没有SO,但是原理是一样的)
是的^^
编辑OP和评论后进行编辑:
不存在“脚本实例”这样的东西:简单地说,向脚本发出请求时发生的情况是:
实际上,您可以让两个用户向同一个 PHP 脚本(或全部包含相同 PHP 文件的不同 PHP 脚本)发送请求;这绝对不是问题,否则我曾经工作过的网站都无法工作!
The server, depending on its configuration, can generally serve hundreds of requests at the same time -- if using Apache, the
MaxClients
configuration option is the one saying :The fact that two clients request the same page is not a problem.
So :
No ; except if :
MaxClients
currently active processes -- see the quote from Apache's manual just before.No : this would mean only one user can use a website at the same time ; this would not be quite nice, would it ?
If it was the case, I could not post this answer, if you where hitting F5 at the same moment to see if someone answered !
(Well, SO is not in PHP, but the principles are the same)
Yes ^^
edit after you edited the OP and the comment :
There is no such thing as "script instance" : put simply, what's happening where a request to a script is made is :
Really, you can have two users sending a request to the same PHP script (or to distinct PHP scripts that all include the same PHP file) ; that's definitly not a problem, or none of the website I ever worked on would work !
如果 2 个客户端同时调用服务器,服务器很可能能够几乎同时回复两个客户端。我在这里将客户端定义为浏览器级别。
意思是说,在同一台机器上,如果您使用两个浏览器同时加载同一个网站/页面,则两个浏览器都应该同时加载。
然而,由于我们讨论的是 PHP,因此您需要特别注意会话。如果您的页面使用会话,则服务器一次仅提供一个页面。这是因为会话文件将被锁定,直到脚本退出。
看这个例子。这两个文件是从同一会话(即同一浏览器同一用户)加载的。
请注意,scriptb.php 仅在提供 scripta.php 后启动。这是因为当 scripta.php 启动时,会话文件被锁定到其他脚本,以便 scripta.php 可以写入会话文件。当 scripta.php 完成时,会话文件被解锁,因此其他脚本可以使用它。因此 scriptb.php 将等待会话文件被释放,然后它将锁定会话文件并使用它。
此过程将不断重复,以防止多个脚本写入同一会话文件导致延迟。因此,建议在不再使用会话时调用
session_write_close
(),尤其是在使用许多 iframe 或 AJAX 的网站上。If 2 clients calls the server at the same time, the server is most probably able to reply both clients almost simultaneously. The clients here I define them to the browser level.
Meaning to say that on the same machine, if you're using 2 browsers to load the same website/page at the same time, both should be loaded at the same time.
however since we're talking about PHP, you need to take special notes about sessions. If your pages use sessions, the server only serve one page at a time. This is because session file will be locked, until a script exits.
Look at this example. The 2 files are loaded from the same session aka same browser same user.
Notice that scriptb.php is only started after scripta.php is served. this is because when scripta.php started, the session file is locked to other scripts so that scripta.php can write to the session file. When scripta.php completes, the session file is unlocked and thus other scripts are able to use it. Thus scriptb.php will wait until the session file is freed then it will lock the session file and use it.
This process will keep repeating to prevent multiple scripts writing to the same session file causing delays. Thus it is recommended to call
session_write_close
() when you are no longer using the session, especially on a website using many iframes or AJAX.我自己刚刚遇到了这个。基本上,您需要调用
session_write_close()
来防止单用户锁定。确保在调用session_write_close()
后,您不会尝试修改任何会话变量。一旦调用它,从那时起将会话视为只读。Just ran into this myself. Basically you need to call
session_write_close()
to prevent single user locking. Make sure once you callsession_write_close()
you don't try and modify any session variables though. Once you call it, treat sessions as read-only from then on.除非您运行的是非常非标准的设置,否则您的 Web 服务器(Apache、IIS、nginx 等)将具有多个进程,这些进程针对进入服务器的每个请求单独运行 PHP。同时请求将同时得到服务。
Unless you are running a very non-standard setup your web server (Apache, IIS, nginx, etc.) will have multiple processes that run PHP separately for each request that comes into the server. Simultaneous requests will be serviced simultaneously.