PHPsp* 连接方法如何工作?

发布于 2024-08-03 18:54:09 字数 91 浏览 3 评论 0原文

我的理解是,PHP 的 p* 连接是在页面加载到服务(无论是内存缓存还是套接字等)之间保持持久连接。但这些连接线程安全吗?当两个页面尝试同时访问同一个连接时会发生什么?

My understanding is that PHP's p* connections is that it keeps a connection persistent between page loads to the service (be it memcache, or a socket etc). But are these connections thread safe? What happens when two pages try to access the same connection at the same time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

甜心小果奶 2024-08-10 18:54:09

在典型的 unix 部署中,PHP 作为一个在 apache Web 服务器内运行的模块安装,该服务器又被配置为将 HTTP 请求分派给多个生成的子级之一。

为了提高效率,apache通常会提前生成这些进程(预先分叉它们)并维护它们,以便它们可以分派多个请求,并节省为每个进来的请求启动一个进程的开销PHP

的工作原理是在干净的环境中启动每个请求;页面加载之间不会保留任何脚本变量。 (与 mod_perl 或 python 相比,应用程序经常由于意外的状态残留而表现出微妙的错误)。

这意味着 PHP 脚本分配的典型资源,无论是 GD 的图像句柄还是数据库连接,都将在请求结束时释放。

某些资源(尤其是 Oracle 数据库连接)的建立成本相当高,因此需要以某种方式缓存已分派的 Web 请求之间的连接。

输入持久资源。

这些工作的方式是,任何给定的 apache 子进程都可以通过将资源注册到资源的“持久列表”中来维护超出请求范围的资源。持久列表在请求结束时不会被清除(内部称为 RSHUTDOWN)。当您使用 pconnect 函数时,它将在持久列表条目中查找一组给定的唯一凭据并返回该条目(如果存在),或者使用这些凭据建立新连接。

如果您已将 apache 配置为维护 200 个子进程,那么您应该会看到从 Web 服务器到数据库计算机建立了许多连接。

如果您有许多 Web 服务器和一台数据库计算机,那么您最终加载的数据库计算机可能会比您预期的要多得多。

对于线程化 SAPI,持久列表是按线程维护的,因此它应该是线程安全的并且具有类似的优点,但是关于不建议在线程化 SAPI 中运行 PHP 的常见警告适用——虽然 PHP 本身是线程安全的,所以很多它使用的库可能有自己的线程安全问题,并给您带来很多麻烦。

In the typical unix deployment, PHP is installed as a module that runs inside the apache web server, which in turn is configured to dispatch HTTP requests to one of a number of spawned children.

For the sake of efficiency, apache will often spawn these processes ahead of time (pre-forking them) and maintain them, so that they can dispatch more than one request, and save the overhead of starting up a process for every request that comes in.

PHP works on the principle of starting every request with a clean environment; no script variables persist between page loads. (Contrast this with mod_perl or python, where applications often manifest subtle bugs due to unexpected state hangovers).

This means that the typical resource allocated by a PHP script, be it an image handle for GD or a database connection, will be released at the end of a request.

Some resources, particularly Oracle database connections, have quite a high cost to establish, so it is desirable to somehow cache that connection between dispatched web requests.

Enter persistent resources.

The way these work is that any given apache child process may maintain a resource beyond the scope of a request by registering it in a "persistent list" of resources. The persistent list is not cleaned up at the end of the request (known as RSHUTDOWN internally). When you use a pconnect function, it will look up the persistent list entry for a given set of unique credentials and return that, if it exists, or establish a new connection with those credentials.

If you have configured apache to maintain 200 child processes, you should expect to see that many connections established from your web server to your database machine.

If you have many web servers and a single database machine, you may end loading your database machine much more than you anticipated.

With a threaded SAPI, the persistent list is maintained per thread, so it should be thread safe and have similar benefits, but the usual caveat about PHP not being recommended to run in threaded SAPI applies--while PHP is itself thread safe, so many libraries that it uses may have thread safety problems of their own and cause you a good number of headaches.

等数载,海棠开 2024-08-10 18:54:09

该手册的页面持久数据库连接可能会为您提供一些有关持久连接。

它仍然没有说明任何有关线程安全的具体内容;据我所知,我从未在任何地方见过任何有关此的内容,所以我认为它“工作正常”。我的猜测是,仅当另一个线程尚未同时使用连接时,才会重新使用连接,但这只是某种(逻辑上的)疯狂猜测......

The manual's page Persistent Database Connections might get you a couple of informations about persistent connections.

It doesn't say anything specific about thread safety, still ; I've quite never seen anything about that anywhere, as far as I remember, so I suppose it "just works OK". My guess would be a connection is re-used only if not already used by another thread at the same time, but it's just some kind of (logical) wild guess...

北陌 2024-08-10 18:54:09

一般来说,PHP 将为 Web 服务器上运行的每个进程或线程建立一个持久连接。因此,一个进程或线程不会访问另一个进程或线程的连接。

相反,当您建立数据库连接时,PHP 将检查数据库连接是否已打开(在处理页面请求的进程或线程中),如果是,则它将使用它,否则它将初始化一个新的连接。

因此,为了回答您的问题,它们不一定是线程安全的,但由于它们的操作方式,不存在两个线程或进程访问同一连接的情况。

Generally speaking, PHP will make one persistent connection per process or thread running on the webserver. Because of this, a process or thread will not access the connection of another process or thread.

Instead, when you make a database connection PHP will check to see if one is already open (in the process or thread that is handling the page request) and if it is then it will use it, otherwise it will just initialize a new one.

So to answer your question, they aren't necessarily thread safe but because of how they operate there isn't a situation where two threads or processes will access the same connection.

相思故 2024-08-10 18:54:09

一般来说,当PHP脚本请求持久连接时,PHP会在连接池中寻找具有相同连接参数的连接。

如果发现一个未被使用,则将其提供给脚本,并在脚本结束时返回到池中。

Generally speaking, when a PHP script requests a persistent connection, PHP will look for one in the connection pool with the same connection parameters.

If one is found that is NOT being used, it is given to the script, and returned to the pool at the end of the script.

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