对 PHP 脚本的同时请求

发布于 2024-08-05 01:39:35 字数 140 浏览 3 评论 0原文

如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫言歌 2024-08-12 01:39:35

服务器根据其配置,通常可以同时处理数百个请求——如果使用 Apache,MaxClients 配置选项是这样说的:

MaxClients 指令设置
同时限制数量
将得到服务的请求。
任意
连接尝试
MaxClients 限制通常为
排队,最多根据
ListenBacklog 指令。
小时候
进程在结束时被释放
不同的请求,连接将
然后进行维修。

两个客户端请求同一页面这一事实并不是问题。

所以 :

请求是否会排队?

不 ;除非:

  • 在某处有一些锁定——例如,如果两个请求来自同一个客户端,并且您在 PHP 中使用基于文件的会话 :在执行脚本时,会话被“锁定”,这意味着服务器/客户端必须等到第一个请求完成(并且文件解锁)才能使用用于为第二个用户打开会话的文件。
  • 请求来自同一客户端和同一浏览器;在这种情况下,大多数浏览器都会对请求进行排队,即使服务器端没有任何东西产生这种行为。
  • 当前活动进程超过 MaxClients - 请参阅前面 Apache 手册中的引用。

他们会被忽略吗?

否:这意味着只有一个用户可以同时使用一个网站;这不太好,不是吗?

如果是这样的话,如果您同时按 F5 看看是否有人回答,我就无法发布此答案!

(PHP中没有SO,但是原理是一样的)

还有其他可能吗?

是的^^

编辑OP和评论后进行编辑:

每个请求都有自己的脚本吗
实例?

不存在“脚本实例”这样的东西:简单地说,向脚本发出请求时发生的情况是:

  • 网络服务器派生另一个进程来处理该请求< em>(通常,出于性能原因,这些分叉是提前进行的,但这不会改变任何事情)
  • 进程从磁盘读取 PHP 脚本
    • 多个进程可以同时执行此操作:文件读取时没有锁定
    • 文件被加载到内存中;每个进程都在不同的内存块中
  • 内存中的 PHP 文件被“编译”为操作码 - 仍然在内存中
  • 执行这些操作码 - 仍然来自属于回答您的进程的内存块请求

实际上,您可以让两个用户向同一个 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 MaxClients directive sets the
limit on the number of simultaneous
requests that will be served.
Any
connection attempts over the
MaxClients limit will normally be
queued, up to a number based on the
ListenBacklog directive.
Once a child
process is freed at the end of a
different request, the connection will
then be serviced.

The fact that two clients request the same page is not a problem.

So :

Will the requests be queued?

No ; except if :

  • there is some lock somewhere -- which can happen, for instance, if the two requests come from the same client, and you are using file-based sessions in PHP : while a script is being executed, the session is "locked", which means the server/client will have to wait until the first request is finished (and the file unlocked) to be able to use the file to open the session for the second user.
  • the requests come from the same client AND the same browser; most browsers will queue the requests in this case, even when there is nothing server-side producing this behaviour.
  • there are more than MaxClients currently active processes -- see the quote from Apache's manual just before.

Will they be ignored?

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)

Any other possibility?

Yes ^^

edit after you edited the OP and the comment :

Will each request have its own script
instance?

There is no such thing as "script instance" : put simply, what's happening where a request to a script is made is :

  • the webserver forks another process to handle the request (often, for performance reasons, those forks are made in advance, but this changes nothing)
  • the process reads the PHP script from disk
    • several processes can do this at the same time : there is no locking on file reading
    • the file is loaded into memory ; in a distinct memory block for each process
  • the PHP file in memory is "compiled" to opcodes -- still in memory
  • those opcodes are executed -- still from the block of memory that belongs to the process answering your request

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 !

输什么也不输骨气 2024-08-12 01:39:35

如果 2 个客户端同时调用服务器,服务器很可能能够几乎同时回复两个客户端。我在这里将客户端定义为浏览器级别。

意思是说,在同一台机器上,如果您使用两个浏览器同时加载同一个网站/页面,则两个浏览器都应该同时加载。

然而,由于我们讨论的是 PHP,因此您需要特别注意会话。如果您的页面使用会话,则服务器一次仅提供一个页面。这是因为会话文件将被锁定,直到脚本退出。

看这个例子。这两个文件是从同一会话(即同一浏览器同一用户)加载的。

      scripta.php requested                 scripta.php served
------+---+---------------------------------+------------------------>
          scripta.php started

               scriptb.php requested           scriptb.php started
---------------+-------------------------------+-----------------+--->
                                                                 scriptb.php served.

请注意,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.

      scripta.php requested                 scripta.php served
------+---+---------------------------------+------------------------>
          scripta.php started

               scriptb.php requested           scriptb.php started
---------------+-------------------------------+-----------------+--->
                                                                 scriptb.php served.

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.

没︽人懂的悲伤 2024-08-12 01:39:35

我自己刚刚遇到了这个。基本上,您需要调用 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 call session_write_close() you don't try and modify any session variables though. Once you call it, treat sessions as read-only from then on.

唔猫 2024-08-12 01:39:35

除非您运行的是非常非标准的设置,否则您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文