Example #1
0
<?php

require '../../vendor/autoload.php';
use CrystalPlanet\Redshift\Redshift;
use CrystalPlanet\Redshift\Stream\Stream;
Redshift::run(function () {
    $stream = stream_socket_server('tcp://0.0.0.0:9000');
    stream_set_blocking($stream, 0);
    $server = new Stream($stream);
    while (true) {
        $client = (yield $server->executeAsyncRead('stream_socket_accept'));
        async(function ($client) {
            $data = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello World!\n";
            fwrite($client, $data);
            fclose($client);
        }, $client);
    }
});
Example #2
0
use CrystalPlanet\Redshift\Channel\Channel;
function fibonacci($c, $quit)
{
    $x = 0;
    $y = 1;
    while (true) {
        list($value, $channel) = (yield Channel::any([$c, $x], $quit));
        switch ($channel) {
            case $c:
                $tmp = $x + $y;
                $x = $y;
                $y = $tmp;
                break;
            case $quit:
                echo $value;
                return;
        }
    }
}
Redshift::run(function () {
    $c = new Channel();
    $quit = new Channel();
    async(function ($c, $quit) {
        for ($i = 0; $i < 10; ++$i) {
            $n = (yield $c->read());
            echo "{$n}\n";
        }
        (yield $quit->write("Quit\n"));
    }, $c, $quit);
    (yield fibonacci($c, $quit));
});
Example #3
0
 /**
  * incremental thingy. that's all.
  */
 function i()
 {
     static $i = 1;
     static $asynci = 100;
     return async() ? $asynci++ : $i++;
 }
Example #4
0
{
    pcntl_waitpid($proc[0], $status);
    // Wait for the process to finish
    // Read the data from the channel, and then unserialize it into
    // a PHP object that can be returned
    $output = unserialize(socket_read($proc[1], 4096));
    socket_close($proc[1]);
    return $output;
}
//
// Usage Example
//
$thread = async(function () {
    // Note that variables can be passed through with the "use" construct
    // (ie. $thread = async(function() use($local_var) { ...
    sleep(2);
    // to show that we really are running asynchronously
    echo "Hello from Child Thread!\n";
    return array('hello', 'world!');
});
echo "Hello from Main Thread\n";
$output = wait($thread);
print_r($output);
// Prints:
//
// Hello from Main Thread
// (and then 2 seconds later)
// Hello from Child Thread
// Array
// (
//     [0] => hello
//     [1] => world!
Example #5
0
 public function exec($h)
 {
     $key = sha1($this->task . $this->action . $h);
     $file = '/home/php/storage/schedule/' . $key;
     if (file_exists($file)) {
         $aged = filemtime($file);
         if ($aged <= time()) {
             touch($file, $this->age);
             if (!$this->closure) {
                 lib('utils')->backgroundTask('thin --task=' . $this->task . ' --action=' . $this->action);
             } else {
                 if (is_callable($this->task)) {
                     async($this->task);
                 } else {
                     lib('utils')->backgroundTask($this->task);
                 }
             }
         }
     } else {
         touch($file, $this->age);
         if (!$this->closure) {
             lib('utils')->backgroundTask('thin --task=' . $this->task . ' --action=' . $this->action);
         } else {
             if (is_callable($this->task)) {
                 async($this->task);
             } else {
                 lib('utils')->backgroundTask($this->task);
             }
         }
     }
 }