Php MySQLi不会在pthreads工作线程中建立连接
为什么在PHP中使用时不能创建连接?当建立mysqli并尝试执行查询时,将生成一个错误 PHP警告:mysqli::query():无法在中获取mysqli 第47行W:\workspace\Sandbox\application.php 这是我的Php MySQLi不会在pthreads工作线程中建立连接,php,mysqli,pthreads,Php,Mysqli,Pthreads,为什么在PHP中使用时不能创建连接?当建立mysqli并尝试执行查询时,将生成一个错误 PHP警告:mysqli::query():无法在中获取mysqli 第47行W:\workspace\Sandbox\application.php 这是我的 以下是一些工作代码 <?php class SQLWorker extends Worker { public function __construct($hostname, $username, $password, $datab
以下是一些工作代码
<?php
class SQLWorker extends Worker {
public function __construct($hostname, $username, $password, $database, $port = 3306) {
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
public function run() {
/* the static class scope acts as a kind of thread local storage for this class */
self::$connection = new MySQLi
($this->hostname, $this->username, $this->password, $this->port);
}
public function getConnection() {
return self::$connection;
}
protected $hostname;
protected $username;
protected $password;
protected $database;
protected $port;
protected static $connection;
}
class SQLTask extends Threaded {
public function __construct($sql) {
$this->sql = $sql;
}
public function run() {
/* fetch static (thread local) connection */
$link = $this->worker->getConnection();
if ($link) {
/* execute query, keep result in local scope */
$result = $link->query($this->sql);
/* build up results as normal array */
while (($row = $result->fetch_assoc())) {
$rows[] = $row;
}
}
/* store results in a fetchable form */
$this->rows = $rows;
}
public function getResults() {
return $this->rows;
}
}
$pool = new Pool(4,
SQLWorker::class,
["localhost", "username", "password", "database"]);
$pool->submit(
$task = new SQLTask("SELECT 1"));
$pool->shutdown();
/* you have your normal array of data here */
var_dump($task->getResults());
第81行是什么?在我的编辑器中是一个空行。@Fred ii-见第47行。我对主题中的脚本做了一些更改。删除了不必要的评论等。您使用的是什么版本的PHP?@Fred ii-PHP5.5.12(cli)(构建时间:2014年4月30日11:20:55)对不起,我不能聊天,我得走了。我会+1看看是否有人能理解。乔,谢谢你。这种线程化对象行为是否会导致实例级对象的丢失?(参见问题中另一位出资人的例子,以及我在回答中的一位。)啊哈,你已经成功了。似乎有两种解决方案-一种是在工作者
内部创建数组/对象静态
,另一种是使用扩展可堆叠的对象
。后者的目的只是为了防止并发线程同时修改共享资源吗?
<?php
class SQLWorker extends Worker {
public function __construct($hostname, $username, $password, $database, $port = 3306) {
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
public function run() {
/* the static class scope acts as a kind of thread local storage for this class */
self::$connection = new MySQLi
($this->hostname, $this->username, $this->password, $this->port);
}
public function getConnection() {
return self::$connection;
}
protected $hostname;
protected $username;
protected $password;
protected $database;
protected $port;
protected static $connection;
}
class SQLTask extends Threaded {
public function __construct($sql) {
$this->sql = $sql;
}
public function run() {
/* fetch static (thread local) connection */
$link = $this->worker->getConnection();
if ($link) {
/* execute query, keep result in local scope */
$result = $link->query($this->sql);
/* build up results as normal array */
while (($row = $result->fetch_assoc())) {
$rows[] = $row;
}
}
/* store results in a fetchable form */
$this->rows = $rows;
}
public function getResults() {
return $this->rows;
}
}
$pool = new Pool(4,
SQLWorker::class,
["localhost", "username", "password", "database"]);
$pool->submit(
$task = new SQLTask("SELECT 1"));
$pool->shutdown();
/* you have your normal array of data here */
var_dump($task->getResults());