asynchronous - PHP CodeIgniter: How to run processes in the background without getting my whole website stuck -


i have website on ubuntu lamp server - has form gets variables , submitted function handles them. function calls other functions in controller "explodes" variables, order them in array , run "for" loop on each variable, gets new data slow apis, , inserts new data relevant tables in database.

whenever submit form, whole website gets stuck (only ip, on other desktops website continue working regularly), , redirected until requested "redirect("new/url);".

i have been researching issue while , found post example: continue php execution after sending http response

after studding how works in server side, explained in video: https://www.youtube.com/watch?v=xvspv-9x3gk

i wanted start learning how write it's syntax , found out work on cli , not apache, wasn't sure.

i opened post few days ago: php+fork(): how run fork in php code

and after getting working server side, installing fork , figuring out differences of php.ini files in server (i edited apache2 php.ini, don't mistaked), stopped getting errors used "fork", processes don't run in background, , didn't redirected.

this controller after adding fork:

<?php     // registers new keyword prod db.  public function add_keyword() {      $keyword_p = $this->input->post('key_word');      $prod      = $this->input->post('prod_name');     $prod      = $this->kas_model->search_prod_name($prod);     $prod      = $prod[0]->prod_id;      $country   = $this->input->post('key_country');      $keyword = explode(", ", $keyword_p);     var_dump($keyword);      $keyword_count = count($keyword);     echo "the keyword count: $keyword_count";      ($i=0; $i < $keyword_count ; $i++) {          // create next fork         $pid = pcntl_fork();          if(!$pid){             //*** new vars $keyword_count             //*** run api functions new data_arrays             //*** inserts new data each $keyword_count db              print "in child $i\n";             exit($i);             // end child         }     }      // parent (main), check child's (optional)     while(pcntl_waitpid(0, $status) != -1){         $status = pcntl_wexitstatus($status);          echo "child $status completed\n";     }      // other main code: redirect main page.      redirect('banana/kas');   } ?> 

and controller without fork:

// registers new keyword prod db.  public function add_keyword() {      $keyword_p = $this->input->post('key_word');      $prod      = $this->input->post('prod_name');     $prod      = $this->kas_model->search_prod_name($prod);     $prod      = $prod[0]->prod_id;      $country   = $this->input->post('key_country');      $keyword = explode(", ", $keyword_p);     var_dump($keyword);      $keyword_count = count($keyword);     echo "the keyword count: $keyword_count";      // problematic part needs forking     ($i=0; $i < $keyword_count ; $i++) {           // new vars $keyword_count         // run api functions new data_arrays         // inserts new data each $keyword_count db       }      // redirect main page.      redirect('banana/kas');   } 

the for ($i=0; $i < $keyword_count ; $i++) { part want running in background because it's taking time.

so now:

  1. how can working way explained? because see, fork isn't i'm looking for, or might doing wrong.

  2. i happy learn new techniques, happy suggestions how can in different ways. self learner, , found out great advantages of node.js exmaple, have worked in case if have learnt it. consider learn working node.js in future. sending server requests , getting responses awesome ;).

***** if there need add more information something, please tell me in comments , add more information post if think it's relevant , missed it.

what you're after queue or job system. there's 1 script running time, waiting do. once original php script runs, adds job list, , can continue it's process normal.

there's few implementations of - take @ https://laravel.com/docs/5.1/queues


Comments