intermezzon/asyncprocess

Execute multiple processes asynchronously

2.0.0 2020-01-10 10:34 UTC

This package is auto-updated.

Last update: 2024-05-14 00:12:23 UTC


README

Helper to start multiple processes simultaneously.

Lets start with an example:

$pool = new \Intermezzon\AsyncProcess\Pool();
$pool->addCommand('php -r "echo \"Start process.\"; sleep(2); echo \"Process ended\";"');

// Wait for all commands to be done
$pool->executeAndWait();

You may start multiple processes simultaneously

$pool = new \Intermezzon\AsyncProcess\Pool();
$pool->addCommand('php -r "echo \"Start process 1.\n\"; sleep(2); echo \"Process 1 ended\n\";"');
$pool->addCommand('php -r "echo \"Start process 2.\n\"; sleep(1); echo \"Process 2 ended\n\";"');

// Wait for all commands to be done
$pool->executeAndWait();

Events

You may also monitor output, errors and handle ended process

$pool = new \Intermezzon\AsyncProcess\Pool();
$pool->addCommand('my_program')
	->started(function ($command)) {
		echo "Process has started\n";
	})
	->output(function ($command, $output) {
		echo "Process outputed:" . $output . "\n";
		// Send stuff to process stdin
		$command->input("send this input to process stdin");
	})
	->error(function ($command, $error) {
		echo "Process send error: " . $error ."\n";
	})
	->ended(function ($command, $exitCode) {
		echo "Process ended with exit code: " . $exitCode . "\n";
		echo "Process took " . $command->totalTime . " seconds to execute.";
	});

// Wait for all commands to be done
$pool->executeAndWait();

Settings

You may want to limit the number of simultaneously running processes so that you do not overload your system.

$pool = new \Intermezzon\AsyncProcess\Pool();
$pool->setMaxProcesses(3);

Kudoz

This has been inspired by