Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

pcntl runs the same code several times assistance required using -'php,forking,pcntl'

pcntl runs the same code several times assistance required  using -'php,forking,pcntl'

I am using pcntl in order to speed up a quite heave CLI php script, that consists mostly of a class, that is in charge of sending all of the auto-emailing on my application.

My goal is as following:
I want to assign each process to a certain task, within a foreach loop, the implementation I've used is the one shown in the code example below.

The problem is that once you fork a process, it executes asynchronously, and also gets a copy of the parent's process stack.
In my case, what happens is that one task simply executes several times,
My question is how can I design this script to be smarter in order to avoid such behavior?.


@description this is the main procedure of this class, it iteratates over the relevant tasks and sends the emails using the SendGrid wrapper class
@see SendGridWrapper
@return void
public function execute(){
        throw new exception("Please call getRelevantTasks() prior to trying to execute anything");
$proccesses = array();
foreach($this->tasks as $myTask){
    $pid = pcntl_fork();
        $proccesses[] = $pid;
    else if($pid == -1){
        die('FORK FAILED, STATUS -1');

            $this->currentModel = $myTask['recipient_model'];
            $lang = $myTask['lang'];
            $classPath = self::$modelsDir . $myTask['recipient_model'] . '.php';
            $className = $myTask['recipient_model'];
            if(!class_exists($myTask['recipient_model'] )){
                require_once(dirname(__FILE__) . '/../' .  $classPath);
            else if(isset($recipientFetcher)){
            $this->model = null;
            $this->mailingList = null;
            $this->substitutionList = null;
            $this->model = new $className($myTask['lang']);
            $addresses = $this->model->getMailRecipients();
            if(empty($addresses) || sizeof($addresses) == 0){
            $this->substitutionList = $this->model->getDynamicParams();

            throw new exception('No recipient model was found');

        foreach($addresses as $myMail){
            $this->mailingList[$myMail['personal_email']] = $myMail['contact_name'];
        $templatePath = dirname(__FILE__) . '/../';
        $templatePath .= $lang ? self::$templatesDirEn . $myTask['html_email_path'] : self::$templatesDirHe . $myTask['html_email_path'];
        $html = file_get_contents($templatePath);
        $this->sendMail($html, $myTask['task_schedule_id']);
        echo "model:" . $myTask['recipient_model'];
        echo $this->log;
        $this->log = "";
        die("\r\n Child proccess has been executed successfully\r\n");  

    foreach($proccesses as $key => $val){
         pcntl_waitpid($val, $status, WUNTRACED);


Thanks in advance,

asked Sep 7, 2015 by rajesh
0 votes

Related Hot Questions

Government Jobs Opening