PHP code example of kesha-dev / yii2-async

1. Go to this page and download the library: Download kesha-dev/yii2-async 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/ */

    

kesha-dev / yii2-async example snippets


[
    'components' => [
        'async' => [
            'class' => 'vxm\async\Async',
            'appConfigFile' => '@app/config/async.php' // optional when you need to use yii feature in async process.
        ]
    ]
]

define('YII_ENV', 'dev');
define('YII_DEBUG', true);

return [
    'id' => 'async-app',
    'basePath' => __DIR__,
    'runtimePath' => __DIR__ . '/runtime',
    'aliases' => [
        '@frontend' => dirname(__DIR__, 2) . '/frontend',
        '@backend' => dirname(__DIR__, 2) . '/backend'
    ]
];


Yii::$app->async->run(function() {
     // do a thing.
});



Yii::$app->async->run(function() {

    if (rand(1, 2) === 1) {
    
        throw new \YourException;
    }
    
    return 123;
}, [
    'success' => function ($result) {
    
        echo $result; // 123
        
    },
    'catch' => function (\YourException $exception) {
        
        // catch only \YourException
        
    },
    'error' => function() {
    
        // catch all exceptions
        
    },
    'timeout' => function() {
    
        // call when task timeout default's 15s
        
    }
]);



Yii::$app->async->run(function() {
    // do a thing.
})->wait();



Yii::$app->async->run([YourAsyncClass::class, 'handleA']);
Yii::$app->async->run([YourAsyncClass::class, 'handleD']);
Yii::$app->async->run([YourAsyncClass::class, 'handleC']);
Yii::$app->async->run([YourAsyncClass::class, 'handleD']);

$results = Yii::$app->async->wait(); // results return from tasks above.



use vxm\async\Task;

class MyTask extends Task
{

    public $productId;
    

    public function run()
    {
        // Do the real work here.
       
    }
}

// Do task async use like an anonymous above.

Yii::$app->async->run(new MyTask([
    'productId' => 123

]));