How to multithread with pThreads in PHP7? workers, pool, etc.

Currently I want to learn and implement pthreads - github . I compiled php7with the necessary components and confirmed that it was thread-safetyincluded. In my current code example, there are 4 child classes that belong to the parent class Canine. How should I call a class Threadto execute bark()from all the classes shown below the same way? Bearing in mind the ability to scale when classes grow from 4 to 100. Pools? Workers?

 class Database {
    private $_host;
    private $_user;
    private $_pass;
    private $_dbname;
    private $_dsn;
    private $_options;
    public static $pdo;
    private $error;

    private $stmt;

    public function __construct() {
        $this->_host   = 'xxxxx';
        $this->_user   = 'xxxxx';
        $this->_pass   = 'xxxxx';
        $this->_dbname = 'xxxxx';
        $this->_dsn    = 'mysql:host=' . $this->_host . ';dbname=' . $this->_dbname;
        // Set options
        $this->_options = array(
            PDO::MYSQL_ATTR_LOCAL_INFILE => true,
            PDO::ATTR_PERSISTENT         => true,
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_TIMEOUT            => 1,
        );
    }

    public function __sleep() {
        return array('_dsn', '_user', '_pass');
    }

    public function __wakeup() {
        $this->connect();
    }

    public function connect() {
        try {
            return self::$pdo = new PDO($this->_dsn, $this->_user, $this->_pass, $this->_options);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }
}

class DataAccessObject {
    protected $database;

    public function __construct() {
        //Create Instance of DB
        $this->database = (new Database())->connect();
    }

    public function insertQuery($query) {
        try {
            $select = $this->database->prepare($query);
            $select->execute();
            $results = $select->fetchAll(PDO::FETCH_ASSOC);
        } catch (Exception $e) {
            echo "Failed: " . $e->getMessage();
        }
        return true;
    }
}

class Helper extends DataAccessObject {
//code elided
}

abstract class Canine extends Helper {
    abstract public function bark();
}

//Child Classes

class GoldenRitriever extends Canine {
    public function bark() { echo 'arf-arf!<br>'; }
}

class Pitbull extends Canine {
    public function bark() { echo 'ruff-ruff!<br>'; }
}

class GermanShepherd extends Canine {
    public function bark() { echo 'wao-wao!<br>'; }
}

class LabradorRetriever extends Canine {
    public function bark() { echo 'woof-woof!<br>'; }
}

Creating instances and invoking `bark()`

$golden_ritriever = new GoldenRitriever();
$golden_ritriever->bark();

$pitbull = new Pitbull();
$pitbull->bark();

$german_shepherd = new GermanShepherd();
$german_shepherd->bark();

$labrador_retriever = new LabradorRetriever();
$labrador_retriever->bark();
+4
source share
1 answer

, , Thread , .

, (Helper, DataAccessObject) DataAcessObject Thread, .

class ThreadedCanine extends Thread {
    private $canine;
    public function __construct(Canine $canine) {
        $this->canine = $canine;
    }

    public function run() {
        $this->canine->bark();
    }
}

$threads = [
    new ThreadedCanine(new GoldenRitriever()),
    new ThreadedCanine(new Pitbull()),
    new ThreadedCanine(new GermanShepherd()),
    new ThreadedCanine(new LabradorRetriever()),
];

foreach($threads as $thread) {
    $thread->start();
}

foreach($threads as $thread) {
    $thread->join();
}

, , Canine , .

, 100 , , Pool Worker ( Worker Thread), 100 ( , ): P

, Threads, .

, :)

+4

Source: https://habr.com/ru/post/1653582/


All Articles