是否有一种实际的方法可以在PHP中实现多线程模型,无论是真正的还是仅对其进行仿真。 一段时间以前,建议您可以强制操作系统加载PHP可执行文件的另一个实例并处理其他同时进行的进程。
这样做的问题是,当PHP代码完成执行PHP实例后,它仍保留在内存中,因为无法从PHP中杀死它。 因此,如果您正在模拟多个线程,则可以想象会发生什么。 因此,我仍在寻找一种可以在PHP中有效完成或模拟多线程的方法。 有任何想法吗?
#1楼
可用的PHP中没有线程,但是可以通过将HTTP请求用作异步调用来进行并发编程。
将curl的超时设置设置为1,并对要彼此关联的进程使用相同的session_id,则可以与会话变量进行通信,如下例所示。 使用这种方法,您甚至可以关闭浏览器,并发进程仍在服务器上。
不要忘记像这样验证正确的会话ID:
startprocess.php
$request = "http://localhost/test/process1.php?sessionid=".$_REQUEST["PHPSESSID"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_exec($ch);
curl_close($ch);
echo $_REQUEST["PHPSESSID"];
process1.php
set_time_limit(0);
if ($_REQUEST["sessionid"])
session_id($_REQUEST["sessionid"]);
function checkclose()
{
global $_SESSION;
if ($_SESSION["closesession"])
{
unset($_SESSION["closesession"]);
die();
}
}
while(!$close)
{
session_start();
$_SESSION["test"] = rand();
checkclose();
session_write_close();
sleep(5);
}
verifysession.php
if ($_REQUEST["sessionid"])
session_id($_REQUEST["sessionid"]);
session_start();
var_dump($_SESSION);
closeprocess.php
if ($_REQUEST["sessionid"])
session_id($_REQUEST["sessionid"]);
session_start();
$_SESSION["closesession"] = true;
var_dump($_SESSION);
#2楼
我知道这是一个老问题,但是对于搜索的人来说,有一个用C编写的PECL扩展现在可以提供PHP多线程功能,它位于此处https://github.com/krakjoe/pthreads
#3楼
可以在php中进行多线程
是的,您可以使用pthreads在PHP中执行多线程
从PHP文档 :
pthreads是一个面向对象的API,它提供了PHP中多线程所需的所有工具。 PHP应用程序可以创建,读取,写入,执行并与线程,辅助对象和线程对象同步。
警告 :pthreads扩展名不能在Web服务器环境中使用。 因此,PHP中的线程应仅保留给基于CLI的应用程序使用。
简单测试
#!/usr/bin/php
<?php
class AsyncOperation extends Thread {
public function __construct($arg) {
$this->arg = $arg;
}
public function run() {
if ($this->arg) {
$sleep = mt_rand(1, 10);
printf('%s: %s -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep);
sleep($sleep);
printf('%s: %s -finish' . "\n", date("g:i:sa"), $this->arg);
}
}
}
// Create a array
$stack = array();
//Initiate Multiple Thread
foreach ( range("A", "D") as $i ) {
$stack[] = new AsyncOperation($i);
}
// Start The Threads
foreach ( $stack as $t ) {
$t->start();
}
?>
首轮
12:00:06pm: A -start -sleeps 5
12:00:06pm: B -start -sleeps 3
12:00:06pm: C -start -sleeps 10
12:00:06pm: D -start -sleeps 2
12:00:08pm: D -finish
12:00:09pm: B -finish
12:00:11pm: A -finish
12:00:16pm: C -finish
第二次跑
12:01:36pm: A -start -sleeps 6
12:01:36pm: B -start -sleeps 1
12:01:36pm: C -start -sleeps 2
12:01:36pm: D -start -sleeps 1
12:01:37pm: B -finish
12:01:37pm: D -finish
12:01:38pm: C -finish
12:01:42pm: A -finish
真实的例子
error_reporting(E_ALL);
class AsyncWebRequest extends Thread {
public $url;
public $data;
public function __construct($url) {
$this->url = $url;
}
public function run() {
if (($url = $this->url)) {
/*
* If a large amount of data is being requested, you might want to
* fsockopen and read using usleep in between reads
*/
$this->data = file_get_contents($url);
} else
printf("Thread #%lu was not provided a URL\n", $this->getThreadId());
}
}
$t = microtime(true);
$g = new AsyncWebRequest(sprintf("http://www.google.com/?q=%s", rand() * 10));
/* starting synchronization */
if ($g->start()) {
printf("Request took %f seconds to start ", microtime(true) - $t);
while ( $g->isRunning() ) {
echo ".";
usleep(100);
}
if ($g->join()) {
printf(" and %f seconds to finish receiving %d bytes\n", microtime(true) - $t, strlen($g->data));
} else
printf(" and %f seconds to finish, request failed\n", microtime(true) - $t);
}
#4楼
可能是我错过了一些东西,但是exec在我在Windows中使用的Windows环境中对我来说不是异步的,它像魅力一样工作;)
$script_exec = "c:/php/php.exe c:/path/my_ascyn_script.php";
pclose(popen("start /B ". $script_exec, "r"));
#5楼
pthreads PECL扩展使使用线程成为可能
http://www.php.net/manual/zh/book.pthreads.php
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3164557