Is there any way to run multiple Controller::run() calls simultaneously ?

Hi folks,

I have an app with console commands tied to CRONTAB. The console command basically queries a table to find out the last time named scripts were run i.e. each time of the scripts in the table is run, it updates the table to reflect when it was last run. Some of these scripts need to be run even minute, some every 5 mins and so on.

So, I have something like this: -

$arrValidScripts = Yii::$app->db->createCommand("

	SELECT script_name

	FROM script




		  (($fltTimeNow + 20) >= (last_attempt_run_date + (frequency_minute * 60)))


		  last_attempt_run_date = 0



	ORDER BY script_name


foreach($arrValidScripts as $arrValidScript):

	$strScriptName = $arrValidScript['script_name'];

	echo $strScriptName;

	exec(Controller::run("console-script/run", $params = [$strScriptName]));


So, that’s all working fine.

Here’s the catch - I REALLY want to be able to run all the scripts simultaneously as some of them taking a fraction of a second, others take over a minute and this is affecting the quality of the service provided by the app. I was lead to believe that using exec() would accomplish this, but it doesnt seem to in this case.

Can anyone help? Or is multithreading just impossible in PHP?



If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

The only way I can think to do what you want is to create an exe or batch file that runs the controller and then invoke this with exec redirecting to null or log etc.

Here is the solution:

exec(Controller::run("console-script/run", $params = [$strScriptName]) . " > /dev/null &");

I would like to emphasize that this is multiprocessing, not multithreading. Though multithreading is possible in PHP as well.