是否有一种可行的方法在PHP中实现多线程模型,无论是真正的,还是只是模拟它。以前曾有人建议,可以强制操作系统加载PHP可执行文件的另一个实例,并同时处理其他进程。

这样做的问题是,当PHP代码完成执行时,PHP实例仍然保留在内存中,因为没有办法从PHP内部杀死它。所以如果你正在模拟几个线程,你可以想象会发生什么。所以我仍然在寻找一种方法,多线程可以在PHP中有效地完成或模拟。什么好主意吗?


当前回答

为什么不用popen?

for ($i=0; $i<10; $i++) {
    // open ten processes
    for ($j = 0; $j < 10; $j++) {
        $pipe[$j] = popen('script2.php', 'w');
    }

    // wait for them to finish
    for ($j = 0; $j < 10; ++$j) {
        pclose($pipe[$j]);
    }
}

其他回答

我知道这是一个老问题,但毫无疑问,PHPThreads对许多人都很有用

代码示例:

function threadproc($thread, $param) {
 
    echo "\tI'm a PHPThread.  In this example, I was given only one parameter: \"". print_r($param, true) ."\" to work with, but I can accept as many as you'd like!\n";
 
    for ($i = 0; $i < 10; $i++) {
        usleep(1000000);
        echo "\tPHPThread working, very busy...\n";
    }
 
    return "I'm a return value!";
}
 

$thread_id = phpthread_create($thread, array(), "threadproc", null, array("123456"));
 
echo "I'm the main thread doing very important work!\n";
 
for ($n = 0; $n < 5; $n++) {
    usleep(1000000);
    echo "Main thread...working!\n";
}
 
echo "\nMain thread done working.  Waiting on our PHPThread...\n";
 
phpthread_join($thread_id, $retval);
 
echo "\n\nOur PHPThread returned: " . print_r($retval, true) . "!\n";

需要PHP扩展:

posix pcntl 套接字

我已经在生产中使用这个库好几个月了。我花了很多精力让它看起来像在使用POSIX pthread。如果您熟悉pthread,那么您可以立即学习并非常有效地使用它。

在计算上,内部工作方式有很大不同,但实际上,功能几乎是相同的,包括语义和语法。

我用它编写了一个非常高效的WebSocket服务器,支持高吞吐率。对不起,我跑题了。我只是很兴奋,我终于把它发布了,我想看看它会帮助谁!

为什么不用popen?

for ($i=0; $i<10; $i++) {
    // open ten processes
    for ($j = 0; $j < 10; $j++) {
        $pipe[$j] = popen('script2.php', 'w');
    }

    // wait for them to finish
    for ($j = 0; $j < 10; ++$j) {
        pclose($pipe[$j]);
    }
}

虽然不能使用线程,但在php中确实有某种程度的进程控制。这里有用的两个函数集是:

过程控制功能 http://www.php.net/manual/en/ref.pcntl.php

POSIX函数 http://www.php.net/manual/en/ref.posix.php

你可以用pcntl_fork来分叉你的进程——返回子进程的PID。然后你可以使用posix_kill来处理这个PID。

也就是说,如果你终止了父进程,就应该向子进程发送一个信号,告诉它终止。如果php本身没有识别这一点,你可以注册一个函数来管理它,并使用pcntl_signal做一个干净的退出。

您可以使用exec()来运行命令行脚本(例如命令行php),如果您将输出管道到一个文件,那么您的脚本将不会等待命令完成。

我不太记得php CLI的语法,但你会想要这样的东西:

exec("/path/to/php -f '/path/to/file.php' | '/path/to/output.txt'");

我认为相当多的共享托管服务器出于安全原因默认禁用了exec(),但可能值得一试。

我知道这是一个老问题,但对于搜索的人来说,有一个用C编写的PECL扩展,现在提供了PHP多线程功能,它位于https://github.com/krakjoe/pthreads