Is there a way to execute internal functions in parallel in PHP?
For example, I have four functions:
function a(){
return 1;
}
function b(){
return 2;
}
function c(){
return 3;
}
function d(){
return 4;
}
When I call these functions sequentially like this:
function E(){
$response = [] ;
$response['a'] = a();
$response['b'] = b();
$response['c'] = c();
$response['d'] = d();
}
They execute in sequential order. However, I want to call them in parallel. Is there any way to achieve this in PHP?
I tried with AMP and swoole php but not worked
1 Answer 1
Swoole can do this. Does not work is not a proper description.
Mutliple corotuines created with go are executed concurrently.
<?php
co::run(function() {
go(function () {
co::sleep(3);
go(function () {
co::sleep(2);
echo "co[3] end\n";
});
echo "co[2] end\n";
});
co::sleep(1);
echo "co[1] end\n";
});
<?php
use Swoole\Coroutine;
function a()
{
echo 1;
sleep(1);
echo __FUNCTION__;
}
function b()
{
echo 2;
sleep(1);
echo __FUNCTION__;
}
function c()
{
echo 3;
sleep(1);
echo __FUNCTION__;
}
function d()
{
echo 4;
sleep(1);
echo __FUNCTION__;
}
Coroutine\run(function () {
Coroutine::create('a');
Coroutine::create('b');
Coroutine::create('c');
Coroutine::create('d');
});
learn more https://openswoole.com/docs/modules/swoole-coroutine-create
1 Comment
Explore related questions
See similar questions with these tags.
Does not workis not a proper description. Show examples and we may assist you.