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 7188971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:12:07+00:00 2026-05-28T19:12:07+00:00

I have code that’s somewhat like this: ($i=0; $i < 100; $i++) { do

  • 0

I have code that’s somewhat like this:

($i=0; $i < 100; $i++)
{
   do ($foo[$i]);
}

The above is a time intensive task, and I was hoping to be able to create a function, and call it twice like below

function wrapper($start;$end)
{
    ($i=$start; $i < $end; $i++)
    {
       do ($foo[$i]);
    }
}
//have both of these run in parallel
wrapper(0,50);
wrapper(51,100);

I looked at Gearman but I cannot use it as I cannot install the gearman server (since I’m on a shared server). It seems like the way to achieve this would be by forking. I tried reading up a lot about it, but documentation and support is scant. Any help / wireframe code would be appreciated.

To define my question, how could I call wrapper() passing in arguments such that it executes in a child process. Also, its important that I be able to register a callback function.

Additional Details: PHP 5.3, running on Linux server. Script is executed by cgi-fcgi.

I think this is how I am supposed to spawn a child process, but how can I use it to spawn multiple child processes? And how do I register a callback function?

$pid = pcntl_fork(); 

if ( $pid == -1 ) {        
    // Fork failed            
    exit(1); 
} else if ( $pid ) { 
    // The parent process
    //should I repeat this same code here to spawn another child process?

} else { 
    // the child process 
    //can I call wrapper from here and will it run in this child process?
  • 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-05-28T19:12:07+00:00Added an answer on May 28, 2026 at 7:12 pm

    From “Tudor Barbu’s professional blog”

    (http://blog.motane.lu/2009/01/02/multithreading-in-php/)

    require_once( 'Thread.php' );
    
    // test to see if threading is available
    if( ! Thread::isAvailable() ) {
        die( 'Threads not supported' );
    }
    
    // function to be ran on separate threads
    function paralel( $_limit, $_name ) {
        for ( $index = 0; $index < $_limit; $index++ ) {
            echo 'Now running thread ' . $_name . PHP_EOL;
            sleep( 1 );
        }
    }
    
    // create 2 thread objects
    $t1 = new Thread( 'paralel' );
    $t2 = new Thread( 'paralel' );
    
    // start them
    $t1->start( 10, 't1' );
    $t2->start( 10, 't2' );
    
    // keep the program running until the threads finish
    while( $t1->isAlive() && $t2->isAlive() ) {
    
    }
    

    Download Thread.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that looks like this: public class A { public void doStuff()
I have code that looks like this: public class Polynomial { List<Term> term =
I have code that looks like this: <div id="header"> <ul class="tabs"> <li><a href="/user/view"><span class="tab">Profile</span></a></li>
I have code that looks like this.... function Finder(id) { this.id = id; this.input
I have code that looks like this: <div class=tag>Order # :</div> <div class=data> <input
I have code that looks something like this: Card m_currentCard; public Card CurrentCard {
I have code that, simplified down, looks like this: run = functools.partial(run, grep=options.grep, print_only=options.print_only,
I have code that references a web service, and I'd like the address of

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.