protected function Pubmsg($s) { $msg = $s . "\r\n"; foreach (Nanoserv::Get_Connections() as $c) { $c->Write($msg); } }
function Countdown() { $this->Write($this->cd--." "); \Nanoserv\Core::New_Timer(0.5, array($this, $this->cd ? "Countdown" : "Disconnect")); }
} // 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++) { My_Worker::$queue->Enqueue(++$b); } $t = time(); } } pecho("work finished in " . number_format(microtime(true) - $start_mt, 2) . " sec"); foreach ($pids as $pid) { posix_kill($pid, SIGTERM); }
#!/opt/local/bin/php <?php require "nanoserv/handlers/HTTP/XML_RPC/Persistent_Server.php"; class dumb_xmlrpc { public $bar; public function getFoo($bar) { return array("foo" => "foo{$bar}", "bar" => $this->bar++); } } $o = new dumb_xmlrpc(); $l = \Nanoserv\Core::New_Listener("tcp://0.0.0.0:800", '\\Nanoserv\\HTTP\\XML_RPC\\Persistent_Server', \Nanoserv\Core::New_Shared_Object($o)); $l->Set_Forking(); $l->Activate(); $o->bar = 100; \Nanoserv\Core::Run();
#!/opt/local/bin/php <?php require "nanoserv/handlers/HTTP/Server.php"; use Nanoserv\Core as Nanoserv; class dumb_httpd extends \Nanoserv\HTTP\Server { public function on_Request($url) { return "You asked for url : <b>{$url}</b>"; } } // Replace [::] below with 0.0.0.0 for IPv4-only operation $l = Nanoserv::New_Listener("tcp://[::]:800", "dumb_httpd"); $l->Set_Forking(); $l->Activate(); Nanoserv::Run();