PHP code example of reactphp-x / process-manager

1. Go to this page and download the library: Download reactphp-x/process-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

reactphp-x / process-manager example snippets


use ReactphpX\ProcessManager\ProcessManager;

$processManager = new ProcessManager(
    'exec php path/to/child_process_init.php',  // 子进程初始化脚本
    1,   // 最小空闲进程数
    1,   // 最大进程数
    100, // 等待队列大小
    10   // 等待超时时间(秒)
);

$fileStream = await($processManager->run(function () {
    return file_get_contents('path/to/file');
}));

$fileStream->on('data', function ($data) {
    echo "File content: " . $data . PHP_EOL;
});

$promiseStream = await($processManager->run(function () {
    return \React\Promise\Timer\sleep(2)->then(function () {
        return 'Hello World';
    });
}));

$promiseStream->on('data', function ($data) {
    echo "Result: " . $data . PHP_EOL;
});

$alwaysStream = await($processManager->run(function ($stream) {
    $timer = Loop::addPeriodicTimer(1, function () use ($stream) {
        $stream->write('Periodic task output\n');
    });
    
    $stream->on('close', function () use ($timer) {
        Loop::cancelTimer($timer);
    });
    
    return $stream;
}));