note 113570 added to class.worker

From: Date: Thu, 31 Oct 2013 03:22:38 +0000
Subject: note 113570 added to class.worker
Groups: php.notes 
Request: Send a blank email to php-notes+get-197355@lists.php.net to get a copy of this message
There's one way to shared datas between Workers, that is using Stackable: <?php 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(); ?> Also you can use $readWorker[] = $some_data; then use $this->shift() in readWorker to share datas with readWorker, but if you do so you can't have variables in readWorker as all variales will be shift by shift(); ---- Server IP: 92.48.119.85 Probable Submitter: 118.113.253.109 ---- Manual Page -- http://php.net/manual/en/class.worker.php Edit -- https://master.php.net/note/edit/113570 Del: integrated -- https://master.php.net/note/delete/113570/integrated Del: useless -- https://master.php.net/note/delete/113570/useless Del: bad code -- https://master.php.net/note/delete/113570/bad+code Del: spam -- https://master.php.net/note/delete/113570/spam Del: non-english -- https://master.php.net/note/delete/113570/non-english Del: in docs -- https://master.php.net/note/delete/113570/in+docs Del: other reasons-- https://master.php.net/note/delete/113570 Reject -- https://master.php.net/note/reject/113570 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#197355) next »