PHP+fork(): How to run a fork in a PHP code PHP+fork(): How to run a fork in a PHP code codeigniter codeigniter

PHP+fork(): How to run a fork in a PHP code


You must mention what's the operating system that you are using, because pcntl extensions are not available on Windows platform
Also you must be aware that activating process control on Linux/Unix within a web server can give you unexpected results, so only CLI/CGI mode is recommended to use PCNTL
Please read carefully this PCNTL Introduction

Now, your code seems to be correct and well implemented, but you must compile your PHP with this option --enable-pcntl to enable pcntl functions like int pcntl_fork(void) otherwise you will get

Fatal error: Call to undefined function pcntl_fork()

For me, the best solution to run functions/methods in asynchronous way is to use pthreads, if you are interested by this advice i can edit my response by adding examples and how to install it on Windows or Linux platforms

Read this article to know how to compile PHP


You have one fork after your pcntl_fork(), check $pid and then you can use it. Repeat for more forks.

<?php    for($i = 1; $i <= 3; ++$i){        // create your next fork        $pid = pcntl_fork();        if(!$pid){            // begin child, your execution code            sleep(1);            print "In child $i\n";            exit($i);            // end child        }    }    // we are the parent (main), check child's (optional)    while(pcntl_waitpid(0, $status) != -1){        $status = pcntl_wexitstatus($status);        echo "Child $status completed\n";    }    // your other main code?>

For async http requests you can use multicurl: http://php.net/manual/en/function.curl-multi-init.php


As far as I am aware of, you can achieve this in two ways...

Use:

  • Pthreads
  • Amphp
  • Pthreads is a parallel processing library, while amp is a pure asynchronous framework...

    So, the way for using pthreads would be to first download/enable the pthreads extension and add extension=/path/to/pthread.so in the php.ini file...

    And then create a class, which extends the Thread Class and override the method run, and put everything inside it, which you want to do parallely.

    So for your specific purpose the the class could be something like this:

    <?phpclass Inserter extends Thread {    public $db_con = null;    public $data;    public function __construct($db_connection, $data) {        $this->db_con = $db_connection;        $this->data = data;    }    private function run() {        // use your logic to insert the data...    }}

    To use that, just instantiate the class, and put the the DB connection variable, and the data to be processed in the ctor. And call the start method of the object. Like:

    $inserter = new Inserter($dbConn, $data);$inserter->start();

    Where $dbConn stores the DB connection and $data stores the necessary data.

    And that's it...