同时请求 PHP 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1430883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:32:29  来源:igfitidea点击:

Simultaneous Requests to PHP Script

phprequest

提问by Kevin Boyd

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?

如果 PHP 引擎已经在服务器上执行脚本的过程中,其他浏览器对同一脚本的并发请求会发生什么情况?

  • Will the requests be queued?
  • Will they be ignored?
  • Will each request have its own script instance?
  • Any other possibility?
  • 请求会排队吗?
  • 他们会被忽视吗?
  • 每个请求都有自己的脚本实例吗?
  • 还有其他可能吗?

回答by Pascal MARTIN

The server, depending on its configuration, can generally serve hundreds of requests at the same time-- if using Apache, the MaxClientsconfiguration option is the one saying :

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

The MaxClientsdirective sets the limit on the number of simultaneous requests that will be served.
Any connection attempts over the MaxClientslimit 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.

MaxClients指令设置了将提供服务的同时请求数量的限制。
任何超过MaxClients限制的连接尝试 通常都会排队,最多可达一个基于 ListenBacklog 指令的数字。
一旦在不同的请求结束时释放子进程,就会为连接提供服务。


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 locksomewhere -- 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 MaxClientscurrently active processes -- see the quote from Apache's manual just before.
  • 某处有一些——这可能发生,例如,如果两个请求来自同一个客户端,并且您在 PHP中使用基于文件的会话:在执行脚本时,会话被“锁定”,这意味着服务器/客户端必须等到第一个请求完成(并且文件已解锁)才能使用该文件为第二个用户打开会话。
  • 请求来自同一个客户端和同一个浏览器;在这种情况下,大多数浏览器都会对请求进行排队,即使服务器端没有产生这种行为。
  • 有比MaxClients当前活跃的进程更多的进程——请参阅之前 Apache 手册中的引用。



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)

如果是这样的话,我不能发布这个答案,如果你在同一时刻按 F5 看是否有人回答!
(好吧,SO 不是在 PHP 中,但原理是一样的)



Any other possibility?

还有其他可能吗?

Yes ^^

是的^^


edit after you edited the OP and the comment :


编辑 OP 和评论后编辑:

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


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 !


实际上,您可以让两个用户向同一个 PHP 脚本(或包含同一个 PHP 文件的不同 PHP 脚本)发送请求;这绝对不是问题,否则我曾经工作过的网站都无法正常工作!

回答by mauris

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.

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

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.

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

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.

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

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.

请注意,scriptb.php 仅在提供 scripta.php 后才启动。这是因为在 scripta.php 启动时,会话文件被其他脚本锁定,以便 scripta.php 可以写入会话文件。当 scripta.php 完成时,会话文件被解锁,因此其他脚本可以使用它。因此 scriptb.php 将等到会话文件被释放,然后它会锁定会话文件并使用它。

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您在不再使用会话时调用(),尤其是在使用大量 iframe 或 AJAX 的网站上。

回答by Justin

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.

我自己就遇到了这个。基本上你需要调用session_write_close()来防止单用户锁定。确保一旦你打电话session_write_close()你就不要尝试修改任何会话变量。一旦你调用它,从那时起将会话视为只读。

回答by conceptDawg

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.

除非您运行非常非标准的设置,否则您的 Web 服务器(Apache、IIS、nginx 等)将有多个进程,这些进程分别为进入服务器的每个请求运行 PHP。并发请求将同时得到服务。