繁体   English   中英

PHP Pthreads - 使用mysqli

[英]PHP Pthreads - using mysqli

我正在尝试在我的Web应用程序中首次使用pthreads。 我有pthreads为简单的测试用例工作,但是在pthreads中使用mysql查询时遇到了麻烦。 这是我的php文件:

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";

这是它的回报:

thread return value:

null


(12) construct finished.
(32) testSqlCall() - sql: SELECT * FROM client_detail
(37) testSqlCall() - total number of returned rows:
(19) testSqlCall() has finished

--- non-threaded return value: 39276

正如您所看到的,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();
?>

产量

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"
}

请注意,MySQLi对象永远不会存储在Threads对象作用域中,因为您只应存储在您要共享的对象作用域中,并且由于您无法共享MySQLi连接,因此最好在方法作用域中对其进行操作。

github上有很多例子,包括一个SQLWorker示例,你应该全部阅读它们。

进一步阅读: https//gist.github.com/krakjoe/6437782

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM