<?php $system = new \Phastlight\System(); $timer = $system->import("timer"); $count = 0; $intervalId = $timer->setInterval(function ($word) use(&$count, &$intervalId, $timer) { $count++; if ($count <= 3) { echo $count . ":" . $word . "\n"; } else { $timer->clearInterval($intervalId); } }, 1000, "world");
<?php use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $system = new \Phastlight\System(); $console = $system->import("console"); $http = $system->import("http"); $http->createServer(function ($req, $res) { $res->writeHead(200, array('Content-Type' => 'text/html')); $request = Request::createFromGlobals(); $response = Response::create("<h1>Hello World</h1>"); $res->end($response->getContent()); })->listen(1337, '127.0.0.1'); $console->log('Server running at http://127.0.0.1:1337/');
<?php $system = new \Phastlight\System(); $console = $system->import("console"); $process = $system->import("process"); $count = 0; $sum = 0; $system->method("sumFromOneToOneHundred", function () use($system, &$count, &$sum) { $console = $system->import("console"); //use the console module $count++; if ($count <= 100000) { $sum += $count; $process = $system->import("process"); //use the process module $process->nextTick(array($system, "sumFromOneToOneHundred")); } else { $console->log("Sum is {$sum}"); } }); $system->sumFromOneToOneHundred(); $console->log("Start Computing Sum From 1 to 100000...");
<?php $system = new \Phastlight\System(); $net = $system->import("net"); $client = $net->connect(array('host' => '127.0.0.1', 'port' => 6379)); $client->on('connect', function () use(&$client) { print "connected to redis server, sending key value over...\n"; $crlf = "\r\n"; $client->write("SET mykey myvalue234{$crlf}"); //we can set the value of a key in one simple command over tcp $client->write("GET mykey{$crlf}"); //we can set the value of a key in one simple command over tcp }); $client->on('data', function ($data) use(&$client) { print_r($data); $client->end(); });
<?php $system = new \Phastlight\System(); $cpu_info = $system->import("os")->getCPUInfo(); $num_of_cpus = count($cpu_info); print $num_of_cpus . "\n";
<?php $system = new \Phastlight\System(); $cluster = $system->import("cluster"); $cluster->fork(function ($worker) { echo "This is worker " . $worker->getProcess()->getPid() . "\n"; $worker->on("close", function ($signal) use(&$worker) { echo "Worker " . $worker->getProcess()->getPid() . " is closed now with signal: {$signal}\n"; }); for (;;) { $worker->kill(); } }, 5); $workers = $cluster->getAllWorkers(); foreach ($workers as $pid => $woker) { print "forked worker with pid: " . $pid . "\n"; }