Ricerca…


Osservazioni

Con pthreads v3 i pthreads possono essere caricati solo quando si usa il cli SAPI, quindi è buona norma mantenere l' extension=pthreads.so direttiva in php-cli.ini SOLO, se si utilizza PHP7 e Pthreads v3.

Se si utilizza Wamp su Windows , è necessario configurare l'estensione in php.ini :

Apri php \ php.ini e aggiungi:

extension=php_pthreads.dll

Per quanto riguarda gli utenti di Linux , devi sostituire .dll di .so :

extension=pthreads.so

Puoi eseguire direttamente questo comando per aggiungerlo a php.ini (cambia /etc/php.ini con il tuo percorso personalizzato)

echo "extension=pthreads.so" >> /etc/php.ini

Iniziare

Per iniziare con il multi-threading, avresti bisogno di pthreads-ext per php, che può essere installato da

$ pecl install pthreads

e aggiungendo la voce a php.ini .

Un semplice esempio:

<?php
// NOTE: Code uses PHP7 semantics.
class MyThread extends Thread {
    /**
     * @var string
     * Variable to contain the message to be displayed.
     */
    private $message;
    
    public function __construct(string $message) {
        // Set the message value for this particular instance.
        $this->message = $message;
    }

    // The operations performed in this function is executed in the other thread.
    public function run() {
        echo $this->message;
    }
}

// Instantiate MyThread
$myThread = new MyThread("Hello from an another thread!");
// Start the thread. Also it is always a good practice to join the thread explicitly.
// Thread::start() is used to initiate the thread,
$myThread->start();
// and Thread::join() causes the context to wait for the thread to finish executing
$myThread->join();

Utilizzo di pool e lavoratori

Il raggruppamento fornisce un'astrazione di livello superiore della funzionalità Worker, inclusa la gestione dei riferimenti nel modo richiesto da pthreads. Da: http://php.net/manual/en/class.pool.php

Pool e lavoratori offrono un livello più elevato di controllo e facilità di creazione di thread multipli

<?php
// This is the *Work* which would be ran by the worker.
// The work which you'd want to do in your worker.
// This class needs to extend the \Threaded or \Collectable or \Thread class.
class AwesomeWork extends Thread {
    private $workName;

    /**
     * @param string $workName
     * The work name wich would be given to every work.
     */
    public function __construct(string $workName) {
        // The block of code in the constructor of your work,
        // would be executed when a work is submitted to your pool.

        $this->workName = $workName;
        printf("A new work was submitted with the name: %s\n", $workName);
    }

    public function run() {
        // This block of code in, the method, run
        // would be called by your worker.
        // All the code in this method will be executed in another thread.
        $workName = $this->workName;
        printf("Work named %s starting...\n", $workName);
        printf("New random number: %d\n", mt_rand());
    }
}

// Create an empty worker for the sake of simplicity.
class AwesomeWorker extends Worker {
    public function run() {
        // You can put some code in here, which would be executed
        // before the Work's are started (the block of code in the `run` method of your Work)
        // by the Worker.
        /* ... */
    }
}

// Create a new Pool Instance.
// The ctor of \Pool accepts two parameters.
// First: The maximum number of workers your pool can create.
// Second: The name of worker class.
$pool = new \Pool(1, \AwesomeWorker::class);

// You need to submit your jobs, rather the instance of
// the objects (works) which extends the \Threaded class.
$pool->submit(new \AwesomeWork("DeadlyWork"));
$pool->submit(new \AwesomeWork("FatalWork"));

// We need to explicitly shutdown the pool, otherwise,
// unexpected things may happen.
// See: http://stackoverflow.com/a/23600861/23602185
$pool->shutdown();


Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow