Yii2 autoloader issue in parallel threads

Hey!

I’m working on a worker manager, which is for executing background tasks with the PHP-cli in a supervisor daemon process. Previously I was using multiple supervisor processes, but because of optimization, I decided to upgrade it to work with multithreading instead of multiprocessing.

For this I’m using the parallel module(https://www.php.net/manual/en/book.parallel.php), but I constantly having an issue with the yii2 autoloader inside of the threads. When you create a Runtime object, then you can also call an autoloader with the constructor. At this point I created an autoloader, which is like in the yii file:

<?php

defined('YII_DEBUG') || define('YII_DEBUG', false);
defined('YII_APP_BASE_PATH') || define('YII_APP_BASE_PATH', dirname(__DIR__, 2));

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../vendor/yiisoft/yii2/Yii.php';
require_once __DIR__ . '/../../common/config/bootstrap.php';
require_once __DIR__ . '/../../console/config/bootstrap.php';

And when call then run function of the Runtime object - which is launching the thread - the loaded classes in the cli process are out of the scope in the thread - this is the reason I’m passing the autoloader to the Runtime constructor.

However for some reason, some classes what I would like to use are not loaded, and in this case it’s crashing the cli process. These classes what I would like to use are just normal classes, what I was using before in the multiprocessing implementation.

    $this->dbQueryClosure = static function ($taskQueue, $mode) {
        // Can see this
        new Autoloader($mode);

        // Can't see this
        $asd = new BaseTask();

        // Can see this
        return (new ThreadedExecutionManagerSearch())->search(['taskQueue' => $taskQueue])->getModels();
    };

This above is an example. That Autoloader class was created only because the configuration should be loaded into the Yii::$app property, and the Application class is also out of scope, unless if I call it from a sub class - aslo dont know why, and it’s not that important, because at least that part is working.

So my question would be, if is there anyone here with some experience with this and has an idea about what could be the issue? Or any explanation why it is not loading some classes I’m using? Or why it loads them - like the Application class - only on specific places? Or some info about it’s functionality could be useful too I guess.

Thanks!

That is very unusual environment you have. Yii was not tested in it so we can’t really help :frowning:

Have you tried https://github.com/yiisoft/yii2-queue?