Наш чат в Telegram для обмена идеями, проектами, мыслями, людьми в сфере ИТ г.Ростова-на-Дону: @it_rostov

The Worker class

(PECL pthreads >= 0.36)

Введение

Worker Threads have a persistent context, as such should be used over Threads in most cases.


Обзор классов


Worker {
/* Методы */
final public boolean chunk ( long $size , boolean $preserve )
final public long getCreatorId ( void )
final public int getStacked ( void )
final public long getThreadId ( void )
final public boolean isShutdown ( void )
final public boolean isWorking ( void )
final public boolean merge ( mixed $from [, mixed $overwrite ] )
final public boolean pop ( void )
abstract public void run ( void )
final public boolean shift ( void )
final public boolean shutdown ( void )
final public int stack ( Stackable $work )
final public boolean start ([ long $options ] )
final public int unstack ([ Stackable $work ] )
}

Содержание


User Contributed Notes 1 note



0
event2game at gmail dot com1 month ago
class data extends Stackable{
    //private $name;
    public function __construct($_name) {
        //$this->name = $_name;//if you set any variable, workers will get the variable, so do not set any variable
        echo __FILE__.'-'.__LINE__.'<br/>'.chr(10);
    }
    public function run(){
        echo __FILE__.'-'.__LINE__.'<br/>'.chr(10);
    }
}
class readWorker extends Worker {
    public function __construct(&$_data) {
        $this->data = $_data;//
    }
    public function run(){
        while(1){
            if($arr=$this->data->shift())//receiving datas
            {
                echo 'Received data:'.print_r($arr,1).chr(10);
            }else usleep(50000);
        }
    }
}
class writeWorker extends Worker {
    public function __construct(&$_data) {
        $this->data = $_data;//
    }
    public function run(){
        while(1){
            $this->data[] = array(time(),rand());//writting datas
            usleep(rand(50000, 1000000));
        }
    }
}
$data = new data('');
$reader = new readWorker($data);
$writer = new writeWorker($data);
$reader->start();
$writer->start();


Описание класса worker, примеры использования класса worker.



Смотрите также:
Описание на ru2.php.net
Описание на php.ru