Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8810089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:02:33+00:00 2026-06-14T03:02:33+00:00

I have around 10 different requests for different servers and used to get the

  • 0

I have around 10 different requests for different servers and used to get the response sequentially in respective to the function call in my website.

Now, I want to call my function in parallel for different server request. Once I get the first response from any of the servers, then want to stop the the remaining processes.

Currently I am calling my function like so:

my_launch(1);
my_launch(2);
my_launch(3);
my_launch(4);
my_launch(5);
my_launch(6);
my_launch(7);
my_launch(8);
my_launch(9);
my_launch(10);  

This executes sequentially; how can I run this code in parallel using PCNTL?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:02:35+00:00Added an answer on June 14, 2026 at 3:02 am

    About PCNTL availibility

    PHP has some PCNTL features built in but they are not enabled by default.

    From php.net pcntl installation instructions:

    You have to compile the CGI or CLI version of PHP with –enable-pcntl configuration option when compiling PHP to enable Process Control support.


    Regarding parallelization

    If you know the number of cores on your machine, then I would suggest dividing the work into that many divisions. Process control only works on *nix-like systems, so you probably have the nproc command available:

    $numberOfProcessors = `nproc`;
    

    You fork to get the proper number of processes running, divide the work between them and off you go. Dividing the work might be difficult, but I’m sure you’ll figure it out.


    Code dump

    I was feeling generous so I went ahead and coded most of it for you. Be sure to understand it because it isn’t quite finished.

    Notes:

    • Grabs the number of available processors by calling nproc, so if you don’t have that utility you should replace it with the number of processes you want to make.
    • If you have more processors available than requested iterations it will not break. It simply uses fewer processors.
    • If the number of processes doesn’t divide evenly into the number of the iterations, the parent process will pick up the remaining iterations.
    • Does not stop after the first result is available. In order to do this, you will need to have one process per iteration. The main process should call pcntl_wait for when the first one finishes and kill the others.

    Code:

    $procs = `nproc`;
    $iterations = 10;
    $pids = array();
    $iproc = -1;
    
    if ($procs > $iterations) {
        $procs = $iterations;
    }
    
    function my_launch($i, $proc) {
        echo "Process $proc ran iteration $i.\n";
    }
    
    $pid = 0;
    for ($i = 0; $i < $procs; $i++) {
        $pid = pcntl_fork();
        if ($pid > 0) {
            // I am the parent process
            $pids[$pid] = $pid;
    
        } elseif ($pid == -1) {
            //error occurred, use available processes only
            $nproc = $i;
            break;
    
        } else {
            //I am the child process.
            $iproc = $i;
            break;
        }
    }
    
    $nproc = !empty($nproc) ? $nproc : $procs;
    
    if ($nproc == 0) {
        echo "NOTICE: Process could not be forked; proceeding in serial.\n";
        for ($i = 0; $i < $iterations; $i++) {
            my_launch($i, $iproc);
        }
        exit;
    }
    
    if ($nproc != $procs) {
        echo "NOTICE: Only using $nproc processes out of the hoped $procs.\n";
    }
    
    $iterationsPerProcess = (int) ($iterations / $nproc);
    
    // children do the main work.
    if ($pid == 0) {
        $stopAt = $iterationsPerProcess * ($iproc + 1);
    
        for ($i = ($iproc * $iterationsPerProcess); $i < $stopAt; $i++) {
            my_launch($i, $iproc);
        }
    }
    
    if ($pid > 0) {
        // parent process picks up the remainder of the work
        $i = $nproc * $iterationsPerProcess;
        for (; $i < $iterations; $i++) {
            my_launch($i, "Main");
        }
    
        //wait for children to finish
        $status = -1;
        foreach ($pids as $createdIndex => $pid) {
            pcntl_waitpid($pid, $status);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have around 40 aspx pages in my website. I want to use a
I have around 2 million strings with different lengths that I need to compress
i have a screen with multiple little widgets (all with different divs around them).
My application has multiple SQLite tables(around 20 with different fields). This I have created
I have around 100 PNGs (that I created) that I want to create an
I have around 100 rows of text that I want to tokenize, which are
I have around 100 models, from MCMCglmm, that give output similar to this: >
I have around 1000 processes which should be executed independently. I have 8 cores.
I have around 1000 html files in my local computer and I have to
I have around 400 GB Live mysql Databases on one server and I like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.