0

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

asked Sep 15, 2023 at 7:44
7
  • PHP doesn't support task-based asynchronous programming, no. Commented Sep 15, 2023 at 7:56
  • Short answer: no, but if you really need to run things in parallel and don't mind taking on the overhead of managing multiple PHP processes, you can do something similar. You can fork separate processes either using php.net/manual/en/function.pcntl-fork.php or your operating systems process control via php.net/exec. Bear in mind these will fork full PHP processes though, and it will be up to you to collect outputs using some kind of inter-process communication. There are libraries and framework components that can help you, depending on what you might be using. Commented Sep 15, 2023 at 8:09
  • 1
    Swoole, pcntl and amp all can do this. Does not work is not a proper description. Show examples and we may assist you. Commented Sep 15, 2023 at 8:22
  • @jimsmith Javascript has no parallelism, but async workflow. It looks similar, but is clearly not the same. Commented Sep 15, 2023 at 8:25
  • Does this answer your question? Executing functions in parallel Commented Sep 15, 2023 at 9:04

1 Answer 1

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

answered Feb 28, 2024 at 7:30
Sign up to request clarification or add additional context in comments.

1 Comment

Please add some explanation to your answer such that others can learn from it

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.