PHP Pthreads-使用mysqli
我第一次尝试在我的web应用程序中使用pthreads。我使用pthreads处理简单的测试用例,但在pthreads中使用mysql查询时遇到了问题。这是我的php文件:PHP Pthreads-使用mysqli,php,mysql,mysqli,pthreads,Php,Mysql,Mysqli,Pthreads,我第一次尝试在我的web应用程序中使用pthreads。我使用pthreads处理简单的测试用例,但在pthreads中使用mysql查询时遇到了问题。这是我的php文件: class SqlThread extends Thread { private $dbc; public $log; public $return; public function __construct(){ $this->dbc = mysqli_connect(
class SqlThread extends Thread
{
private $dbc;
public $log;
public $return;
public function __construct(){
$this->dbc = mysqli_connect("localhost", "root", "rootpassword", "my_database");
$this->log = "<br>(".__LINE__.") construct finished.";
}
//gets called when thread is started
public function run(){
$val = $this->testSqlCall();
$this->log .= "<br>(".__LINE__.") testSqlCall() has finished";
$this->return = $val;
}
/**
* testing sql queries
*/
function testSqlCall(){
$sql = "SELECT *
FROM client_detail";
$this->log .= "<br>(".__LINE__.") testSqlCall() - sql: ".$sql;
$r= @mysqli_query($this->dbc,$sql);
$num = mysqli_affected_rows($this->dbc);
$this->log .= "<br>(".__LINE__.") testSqlCall() - total number of returned rows: ".$num;
return $num;
}
}
/**
* threaded test
*/
$thread = new SqlThread();
$thread->start();
$thread->join();
echo "<br><br><br>thread return value:";
var_dump($thread->return);
echo $thread->log;
/**
* same test, but not threaded
*/
$dbc = mysqli_connect("localhost", "root", "rootpassword", "my_database");
$sql = "SELECT *
FROM client_detail";
$r = @mysqli_query($dbc,$sql);
$num = mysqli_affected_rows($dbc);
echo "<br><br> --- non-threaded return value: $num";
如您所见,SqlThread内的mysqli查询不返回任何内容,而SqlThread类外的完全相同的查询返回我所期望的内容
有什么想法吗?有人在php线程中使用sql查询吗?问题是mysqli对象不适合在多个线程中使用,您希望为启动的每个线程创建一个mysqli实例,因此每个线程都有一个唯一的连接
<?php
define("SQLHOST", "localhost");
define("SQLUSER", "root");
define("SQLPASS", "");
define("SQLDB", "test");
define("SQLPORT", 3306);
define("SQLSOCK", "/var/lib/mysql/mysql.sock");
class Mine extends Thread {
public function run() {
try {
$my = new mysqli(SQLHOST, SQLUSER, SQLPASS, SQLDB, SQLPORT, SQLSOCK);
if ($my) {
$result = $my->query("SHOW DATABASES;");
if (is_object($result)) {
while (($row = $result->fetch_assoc())) {
var_dump($row);
}
}
}
} catch(Exception $ex) {
var_dump($ex);
}
}
}
$mine = new Mine();
$mine->start();
?>
请注意,MySQLi对象永远不会存储在Threads对象范围内,因为您应该只在对象范围内存储要共享的内容,并且由于您无法共享MySQLi连接,因此最好在方法范围内对其进行操作
github上有许多示例,包括一个SQLWorker示例,您应该全部阅读
进一步阅读:您需要在线程内部建立连接。在外部实例化它将使每个线程使用相同的连接,这是不推荐的,如果没有错误的话,在任何多线程体系结构中也是如此。我第一次发现这一点是在用Python进行多线程数据库查询时。
<?php
define("SQLHOST", "localhost");
define("SQLUSER", "root");
define("SQLPASS", "");
define("SQLDB", "test");
define("SQLPORT", 3306);
define("SQLSOCK", "/var/lib/mysql/mysql.sock");
class Mine extends Thread {
public function run() {
try {
$my = new mysqli(SQLHOST, SQLUSER, SQLPASS, SQLDB, SQLPORT, SQLSOCK);
if ($my) {
$result = $my->query("SHOW DATABASES;");
if (is_object($result)) {
while (($row = $result->fetch_assoc())) {
var_dump($row);
}
}
}
} catch(Exception $ex) {
var_dump($ex);
}
}
}
$mine = new Mine();
$mine->start();
?>
array(1) {
["Database"]=>
string(18) "information_schema"
}
array(1) {
["Database"]=>
string(5) "mysql"
}
array(1) {
["Database"]=>
string(18) "performance_schema"
}
array(1) {
["Database"]=>
string(4) "test"
}