您正在寻找的内容只能在具有包含过程控制功能(PCNTL 库)的 PHP 构建的 Linux 风格系统上完成。
你会在这里找到它的文档:http:
//php.net/manual/en/book.pcntl.php
具体来说,您想要做的是“分叉”一个进程。这将创建当前 PHP 脚本进程的相同副本,包括所有内存引用,然后允许两个脚本继续同时执行。
“父”脚本知道它仍然是主脚本。并且“孩子”脚本(或脚本,您可以根据需要多次执行此操作)知道这是一个孩子。这允许您在孩子被分离并变成守护程序后为父母和孩子选择不同的操作。
为此,您将使用以下内容:
$pid = pcntl_fork(); //store the process ID of the child when the script forks
if ($pid == -1) {
die('could not fork'); // -1 return value means the process could not fork properly
} else if ($pid) {
// a process ID will only be set in the parent script. this is the main script that can output to the user's browser
} else {
// this is the child script executing. Any output from this script will NOT reach the user's browser
}
这将使脚本能够衍生出一个子进程,该子进程可以在父脚本的旁边(或很久之后)继续执行,输出它的内容并退出。
您应该记住,这些函数必须编译到您的 PHP 构建中,并且绝大多数托管公司不允许在其服务器上访问它们。为了使用这些功能,您通常需要拥有虚拟专用服务器 (VPS) 或专用服务器。甚至云托管设置通常也不会提供这些功能,因为如果使用不当(或恶意)它们很容易使服务器瘫痪。