Hello,
i want to run php code asynchronously and would therefore like to test the extension yii2-async.
Since I’ve never installed an extension in yii2, I need a little help with this.
I run
composer require vxm/yii2-async
in my root dir and have verified that the installation with composer was successful. The extension was successfully installed under projectRoot/vendor/vxm.
After that i edited my application configure file projectRoot/config/web.php.
This is a snipped of web.php
<?php
$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'components' => [
# A Custom Class. Ignore this.
'MyClass' => [
'class' => 'app\vendor\Myclass',
],#That's the change I've made
'async' => [
'class' => 'vxm\async\Async',
'appConfigFile' => '@app/config/async.php' // optional when you need to use yii feature in async process. (i want)
],
So far everything should be correct and working in my opinion.
Finally i created this action:
public function actionTest() {
echo '1';
Yii::$app->async->run(function() {
return 1337;
}, [
'success' => function ($result) {
echo $result;
},
'catch' => function (\YourException $exception) {
// catch only \YourException
echo 'catch';
},
'error' => function() {
echo 'error';
},
'timeout' => function() {
echo "timeout";
}
]);
echo '2';
#return $this->render('test.php', []);
}
This Action outputs:
12
error
Expected Output:
121337
is there a way to catch the error or to get a detailed error message?
I tried to run the “return 1337;” inside a try, catch, finally block, but the catch and finally block isnt executed.
i modified my index.php to allow error messages.
<?php
error_reporting(E_ALL);
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/../config/web.php';
(new yii\web\Application($config))->run();
Do you have any ideas?
EDIT:
After doing a google research i found out that pcntl has to be activated.
according to phpini pcntl is activated.
My PHP Version is 7.2.34.