예제 #1
0
function concurrentExec($cmd)
{
    $chn = make_channel();
    $chnDone = make_channel();
    echo 'Starting ' . FMTWORKERS . '...', PHP_EOL;
    for ($i = 0; $i < FMTWORKERS; ++$i) {
        cofunc(function ($chn, $chnDone, $cmd, $i) {
            while (true) {
                $str = $chn->out();
                if (null === $str) {
                    break;
                }
                passthru(sprintf($cmd, $str) . ' | while read line; do echo "' . ($i + 1) . ' $line"; done');
            }
            $chnDone->in('OK');
        }, $chn, $chnDone, $cmd, $i);
    }
    return [$chn, $chnDone];
}
예제 #2
0
    }
}
$suffix =& $opt['suffix'];
$pass = new Build($suffix);
$chn = make_channel();
$chn_done = make_channel();
$workers = 2;
echo 'Starting ', $workers, ' workers...', PHP_EOL;
for ($i = 0; $i < $workers; ++$i) {
    cofunc(function ($pass, $chn, $chn_done) {
        while (true) {
            $target = $chn->out();
            if (empty($target)) {
                break;
            }
            echo $target, PHP_EOL;
            file_put_contents($target . '.php', $pass->format(file_get_contents($target . '.src.php')));
            if (file_exists($target . '.stub.src.php')) {
                file_put_contents($target . '.stub.php', $pass->format(file_get_contents($target . '.stub.src.php')));
            }
        }
        $chn_done->in('done');
    }, $pass, $chn, $chn_done);
}
$targets = ['fmt', 'refactor'];
foreach ($targets as $target) {
    $chn->in($target);
}
for ($i = 0; $i < $workers; ++$i) {
    $chn->in(null);
}
for ($i = 0; $i < $workers; ++$i) {
예제 #3
0
파일: example.php 프로젝트: straiway/fmt
function GoogleConcurrentFirst($qry)
{
    $chn = make_channel();
    cofunc(function ($chn, $qry) {
        $chn->in(First($qry, 'Web1', 'Web2', 'Web3'));
    }, $chn, $qry);
    cofunc(function ($chn, $qry) {
        $chn->in(First($qry, 'Image1', 'Image2', 'Image3'));
    }, $chn, $qry);
    cofunc(function ($chn, $qry) {
        $chn->in(First($qry, 'Video1', 'Video2', 'Video3'));
    }, $chn, $qry);
    $results = [];
    for ($i = 0; $i < 3; ++$i) {
        $results[] = $chn->out();
    }
    $chn->close();
    return $results;
}
예제 #4
0
 cofunc(function ($fmt, $backup, $cache_fn, $chn, $chn_done, $lintBefore, $dryRun) {
     $cache = new Cache($cache_fn);
     $cacheHitCount = 0;
     $cache_miss_count = 0;
     $filesChanged = false;
     while (true) {
         $msg = $chn->out();
         if (null === $msg) {
             break;
         }
         $target_dir = $msg['target_dir'];
         $file = $msg['file'];
         if (empty($file)) {
             continue;
         }
         if ($lintBefore && !lint($file)) {
             fwrite(STDERR, 'Error lint:' . $file . PHP_EOL);
             continue;
         }
         $content = $cache->is_changed($target_dir, $file);
         if (false === $content) {
             ++$cacheHitCount;
             continue;
         }
         ++$cache_miss_count;
         $fmtCode = $fmt->formatCode($content);
         if (null !== $cache) {
             $cache->upsert($target_dir, $file, $fmtCode);
         }
         if ($dryRun) {
             if ($fmtCode !== $content) {
                 $filesChanged = true;
             }
         } else {
             file_put_contents($file . '-tmp', $fmtCode);
             $oldchmod = fileperms($file);
             $backup && rename($file, $file . '~');
             rename($file . '-tmp', $file);
             chmod($file, $oldchmod);
         }
     }
     $chn_done->in([$cacheHitCount, $cache_miss_count, $filesChanged]);
 }, $fmt, $backup, $cache_fn, $chn, $chn_done, $lintBefore, $dryRun);
예제 #5
0
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
include "csp.php";
$chn = make_channel();
cofunc(function ($chn) {
    sleep(5);
    $chn->in('timeout');
}, $chn);
$chn2 = make_channel();
cofunc(function ($chn2) {
    sleep(rand(3, 6));
    $chn2->in('action taken');
}, $chn2);
$chn3 = make_channel();
cofunc(function ($chn3) {
    sleep(4);
    echo 'Got: ', $chn3->out(), PHP_EOL;
}, $chn3);
while (true) {
    select_channel([[$chn, function ($msg) {
        echo "Message Received 1:", print_r($msg, true), PHP_EOL;
        die;
    }], [$chn2, function ($msg) {
        echo "Message Received 2:", print_r($msg, true), PHP_EOL;
    }], [$chn3, 'Hello World', function () {
        echo "Sent HW", PHP_EOL;
    }]]);
}
// $chn->close();
// $chn2->close();
// $chn3->close();