I’m using Amphp Parallel. I want to create a task, submit it for execution, and be able to check the execution status without calling await().
For example: a string is passed to a task, the executor falls asleep for 5 seconds, then writes the message “End sleep!” and returns a message that the task is completed.
Here is a working code example using PHP parallel
<?
$executor = function(string $task) {
sleep(5);
print_r('End sleep!' . PHP_EOL);
return $task . ' is completed!';
};
$task = 'My new task';
$runtime = new parallelRuntime();
$future = $runtime->run($executor, [$task]);
$time = time();
while(true) {
$isComplete = $future->done();
print_r('Time: ' . (time() - $time) . ' - State: ' . ($isComplete ? 'true' : 'false') . PHP_EOL);
if($isComplete) {
print_r('Result: ' . $future->value() . PHP_EOL);
break;
}
sleep(1);
}
Result of executing the code:
Time: 0 - State: false
Time: 1 - State: false
Time: 2 - State: false
Time: 3 - State: false
Time: 4 - State: false
End sleep!
Time: 5 - State: true
Result: My new task is completed!
It works as i described above.
Now i want to do the same using Amphp Parallel.
Main script code:
<?
require_once __DIR__ . '/vendor/autoload.php';
use AmpFuture;
use AmpParallelWorker;
use MyTasksMyTask;
$task = 'My new task';
$execution = Workersubmit(new MyTask($task));
$future = $execution->getFuture();
$time = time();
while(true) {
$isComplete = $future->isComplete();
print_r('Time: ' . (time() - $time) . ' - State: ' . ($isComplete ? 'true' : 'false') . PHP_EOL);
if($isComplete) {
print_r('Result: ' . $future->await() . PHP_EOL);
break;
}
sleep(1);
}
Code MyTasksMyTask:
<?
namespace MyTasks;
use AmpParallelWorkerTask;
use AmpSyncChannel;
use AmpCancellation;
class MyTask implements Task {
public function __construct(private readonly string $task) {}
public function run(Channel $channel, Cancellation $cancellation): string {
sleep(5);
print_r('End sleep!' . PHP_EOL);
return $this->task . ' is completed!';
}
}
The result of running this code:
Time: 0 - State: false
Time: 1 - State: false
Time: 2 - State: false
Time: 3 - State: false
Time: 4 - State: false
End sleep!
Time: 5 - State: false
Time: 6 - State: false
Time: 7 - State: false
Time: 8 - State: false
...
The message “End sleep!” says that the task is completed, but $future->isComplete() continues to return false.
If i rewrite the main script like this:
<?
require_once __DIR__ . '/vendor/autoload.php';
use AmpFuture;
use AmpParallelWorker;
use MyTasksMyTask;
$task = 'My new task';
$execution = Workersubmit(new MyTask($task));
$future = $execution->getFuture();
$time = time();
while(true) {
$result = $future->await();
$isComplete = $future->isComplete();
print_r('Time: ' . (time() - $time) . ' - State: ' . ($isComplete ? 'true' : 'false') . PHP_EOL);
if($isComplete) {
print_r('Result: ' . $result . PHP_EOL);
break;
}
sleep(1);
}
I called $future->await() before $future->isComplete() and got this result:
End sleep!
Time: 5 - State: true
Result: My new task is completed!
$future->isComplete() returned true after calling $future->await(), but $future->await() blocked the script from executing.
My ultimate goal is to write a task manager that will distribute tasks to employees and then poll them, get the result and give a new task. Therefore, the blocking that await() does really bothers me.
Can I implement this using Amphp Parallel?
Thanks for any help!
110nn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.