self::Do_Work($data); $processed = 1; } else { // Or sleep if there's nothing to do usleep(50000); $processed = 0; } } } } // The shared object must be created before Nanoserv::Fork()ing My_Worker::$queue = Nanoserv::New_Shared_Object(new My_Queue()); pecho("master process launched, forking " . NUM_CHILDREN . " worker(s) ..."); $pids = array(); for ($a = 0; $a < NUM_CHILDREN; $a++) { if (($pid = Nanoserv::Fork()) === 0) { // Run the child process My_Worker::Run(); } else { $pids[] = $pid; } } pecho("worker(s) ready (" . implode(",", $pids) . ")"); $t = time(); $b = 0; $start_mt = microtime(true); while (My_Worker::$queue->num_done < WORK_LOAD) { Nanoserv::Run(10); if (time() !== $t || count(My_Worker::$queue->elements) < NUM_CHILDREN) { // Fill the queue every second or when it's almost empty for ($a = 0; $a < NUM_CHILDREN * 2 && $b < WORK_LOAD; $a++) {
public function set($v) { if (!($v % 1000)) { pecho("setting value to {$v}"); } $this->value = $v; } public function done($t) { pecho("10000 IPC ops in " . sprintf("%.2f", $t) . " sec."); exit; } } $o = new foo(); $so = \Nanoserv\Core::New_Shared_Object($o); pecho("master process launched, forking"); if (\Nanoserv\Core::Fork() > 0) { \Nanoserv\Core::Run(); } pecho("child process launched"); $t = microtime(true); for ($a = 1; $a <= 5000; $a++) { $so->set($a); if (($test = $so->value) !== $a) { pecho("ERROR: {$test} != {$a}"); } if (!($a % 1000)) { pecho('$so->value = ' . $test); } } $so->done(microtime(true) - $t);