PHP,暂停脚本执行,直到另一个正在运行的脚本发出事件信号?

发布于 2024-10-16 08:12:01 字数 385 浏览 3 评论 0原文

我希望一个 PHP 脚本等待另一个脚本执行期间发送的事件。

用户 A 和用户 B 都是我网站的访问者。当用户A访问index.php时,页面开始执行,但直到用户B也访问index.php后才返回。 (仅当两个用户都连接到 Web 服务器时才会显示该页面,例如:就像两个警卫同时转动钥匙打开保险箱。)

实现此目的的一种方法是让 index.php 暂停其执行直到收到由其他用户访问index.php 引起的通知。

为此,我查看了

  1. libevent;但它似乎不允许在信令期间发送“ID”或“字符串”。
  2. 来自 pear 的 Event_Dispatcher;但它在“脚本之间”不起作用。
  3. PHP共享内存函数;但似乎不能等到内存被改变。

I want one PHP script to wait for an event sent during the execution of another script.

User A and User B are both visitors of my website. When user A visits index.php, the page begins execution but does not return until User B also visit index.php. (The page is only displayed when both user are connected to the web server, ex.: like two guards opening a safe by turning the key at the same time.)

One way to make this happen is for index.php to pause its execution until a notification is received caused by the other user's visit to index.php.

To do this, I have looked at

  1. libevent; But it does not seem to allow "ID" or "strings" to be sent during signaling.
  2. Event_Dispatcher from pear; But it does not work "between" scripts.
  3. PHP shared memory functions; But does not appear to be able to wait until the memory is changed.

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

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

发布评论

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

评论(4

囍孤女 2024-10-23 08:12:01

您可能不希望 PHP 脚本完全暂停。用户会坐在那里以为页面冻结了。相反,我会使用 AJAX 将 USER-A 的状态保存在数据库中,然后使用 AJAX 再次检查 USER-B 是否也保存了其状态。这个想法是让 AJAX 在后台默默地检查 USER-B 的状态。当它检测到登录时,让您的回调函数对页面执行一些操作!

You probably wouldn't want the PHP script to pause completely. The user would kinda just sit there thinking the page froze. Instead, I would use AJAX to save the state of USER-A in a datbase and then use AJAX to again check if USER-B has had their state saved as well. The ideas is to have AJAX silently checking the status of USER-B in the background. When it does detect a login, have your callback function do stuff to the page!

薄凉少年不暖心 2024-10-23 08:12:01

在“等待”请求(用户 A)的代码中,创建一个新的监听套接字 (http ://php.net/manual/en/sockets.examples.php)绑定到不会与其他任何内容冲突的本地主机地址和端口(例如,127.12.34.56 端口 7890),然后等待通过调用socket_accept建立连接。如果已经有一个请求在该状态下等待,则该请求将会失败,否则它将在那里等待连接。

在您的“信令”请求(用户 B)中,创建一个新套接字,并连接到您的侦听套接字。等待用户 A 请求的 PHP 处理程序将立即从它被阻塞的 socket_accept 调用中返回,您可以继续。如果您需要在两者之间传递任何消息,您只需在套接字中读取和写入消息即可。

对于聊天室和显示服务器发送事件的通知页面等应用程序来说,这是一项非常常见的任务。与轮询方法相比,这种方法有一些优点,轮询方法不断调用 sleep 并循环检查数据库或文件(或者从 JavaScript 端循环向服务器发出许多请求):

- 它不会创建不必要的 HTTP 请求或消耗资源和网络流量的 DB 查询或 CPU 循环

- 当触发事件发生时立即采取行动,没有任何延迟

- 它不依赖于 DB 或文件

主要缺点是您的 PHP 必须具有打开套接字并连接到它们的权限,在共享托管环境中通常不是这种情况。

有关示例,请参阅 https://gist.github.com/root9b/e1f0b82769296b06c079e53c7362bb94

In the code for your "waiting" request (User A), create a new listening socket (http://php.net/manual/en/sockets.examples.php) bound to a localhost address and port that won't conflict with anything else (for example, 127.12.34.56 port 7890) then wait for a connection by calling socket_accept. If there is already a request waiting in that state, this will fail otherwise it will wait there for a connection.

In your "signaling" request (User B), create a new socket, and connect to your listening socket. The PHP handler waiting for User A's request will immediately return from the socket_accept call it was blocked in and you can continue. If you need to pass any messages between the two, you can simply read and write the messages to and from the sockets.

This is a very common task to perform for applications like chat rooms and notification pages that display server-sent events. This approach has a few advantages over a polling approach that keeps calling sleep and checking a DB or file in a loop (or makes many requests back to the server in a loop from the JavaScript side):

-It does not create unnecessary HTTP requests or DB queries or CPU loops that consume resources and network traffic

-It acts immediately when the trigger event happens without any delay

-It does not depend on a DB or file

The main disadvantage is that your PHP must have permissions to open sockets and connect to them, which is often not the case in shared hosting environments.

For an example, see https://gist.github.com/root9b/e1f0b82769296b06c079e53c7362bb94

极度宠爱 2024-10-23 08:12:01

使用 sleep 和 db 怎么样?

客户端 1 登录,PHP 插入一个“等待条目”,然后以设定的时间间隔(可能使用 time_nanosleep)轮询数据库以获取标记为“完成”的内容。
客户端2登录,PHP更新“等待条目”,将其设置为“完成”。
客户端 1 看到该过程已完成,继续愉快地前进。

如果您没有数据库,可以使用简单的临时文件来完成。

What about using sleep and a db?

Client 1 logs in, PHP inserts a "waiting entry" and then polls the db at a set interval (maybe using time_nanosleep) for something which has marked it "complete".
Client 2 logs in, PHP updates the "waiting entry" to set it to "complete".
Client 1, seeing that the process is complete, goes on its merry way.

If you don't have a database, this could be done with a simple temp file.

°如果伤别离去 2024-10-23 08:12:01

您正在寻找守护进程,有一些如何创建它的信息:

http://kevin.vanzonneveld。网/techblog/article/create_daemons_in_php/

you are looking for daemon, there some info how to create this:

http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/

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