У меня есть pthreaded пример с MySQL из https://github.com/krakjoe/pthreads/blob/master/examples/MySQLi.php
Я хочу реализовать Json-RPC внутри поточной функции для выполнения некоторых запросов к RPC-серверу.
<?php
require_once '/var/pthreads/vendor/autoload.php';
use JsonRPC\Client;
class Connect extends Worker {
public function __construct($hostname, $username, $password, $database, $port = 3306) {
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->port = $port;
}
public function getConnection() {
if (!self::$link) {
self::$link = new mysqli(
$this->hostname,
$this->username,
$this->password,
$this->database,
$this->port);
}return self::$link;
}
public function getClient() {
if (!self::$client) {
self::$client = new Client('Connection string');
}return self::$client;
}
protected $hostname;
protected $username;
protected $password;
protected $database;
protected $port;
protected static $link;
protected static $client;
}
class Query extends Threaded {
private $id;
public function __construct() {
}
public function run() {
$mysqli = $this->worker->getConnection();
$client = $this->worker->getClient();
echo $client->execute('func');
}
}
$results = array();
$t=4;
$start = microtime(true);
$threads=$t;
$mysqli = new mysqli();
$mysqli->query("TRUNCATE TABLE test_db");
$mysqli->close();
$pool = new Pool($threads, "Connect", ['mysql_params']);
for ($i = 0; $i < 5; ++$i) {
$pool->submit(new Query());
}
$pool->collect();
$pool->shutdown();
$results[$t]['time']=microtime(true)-$start;
print_r($results);
?>
Но я получил эту ошибку:
Fatal error: Uncaught Error: Class 'JsonRPC\Client' not found in /var/pthreads/1.php:33
Stack trace:
#0 /var/pthreads/1.php(56): Connect->getClient()
#1 [internal function]: Query->run()
#2 {main}
thrown in /var/pthreads/1.php on line 33
P.S используя PHP7.2 ZTS и этот JSON-RPC https://github.com/fguillot/JsonRPC
Задача ещё не решена.
Других решений пока нет …