PHP + fork (): Wie man einen Fork in einem PHP-Code ausführt

ch führe meinen Code auf CodeIgniter - Ubuntu Server au

ch habe nach asynchronen Methoden gesucht, um Funktionen auszuführe

Das ist meine Funktion:

<?php   

    // Registers a new keyword for prod to the 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 that needs forking
        for ($i=0; $i < $keyword_count ; $i++) { 

            // get new vars from $keyword_count
            // run API functions to get new data_arrays
            // inserts new data for each $keyword_count to the DB 

        }

        // Redirect to main page. 
        redirect('banana/kas'); 

    }

Foreach verwendet Variablen mit langsamen APIs und aktualisiert die Datenbank.

Ich habe einige Tutorials mit Fork gesehen, habe aber den Syntax-Teil nicht ganz verstanden. Die meisten Dinge, die ich fand, waren nur Erklärungen, wie es funktioniert (2 Prozesse: Eltern-Kind-Effekt), gaben aber keine gute Erklärung, wie dies auf den Code angewendet wird.

Kann jemand erklären, wie ich mit der fork () -Syntax arbeite?

PHP-Ausführung nach dem Senden der HTTP-Antwort fortsetzen

http: //www.onlinetechtutorials.com/2014/06/how-to-run-php-code-asynchronously.htm

http: //php.net/manual/en/function.pcntl-fork.ph (allgemeiner

Serverseitig:https: //www.youtube.com/watch? v = xVSPv-9x3gk

BEARBEITE:

Habe ich es richtig gesagt

<?php   

// Registers a new keyword for prod to the 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";

    for ($i=0; $i < $keyword_count ; $i++) { 
        // create your next fork
        $pid = pcntl_fork();

        if(!$pid){
            //*** get new vars from $keyword_count
            //*** run API functions to get new data_arrays
            //*** inserts new data for each $keyword_count to the DB 
            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: Redirect to main page. 
    redirect('banana/kas'); 

}
?>

Dies wird kein Problem damit verursachen, dies in einer Schleife zu verwenden? Wird es wissen, ob jeder Prozess gestapelt werden muss?

Antworten auf die Frage(6)

Ihre Antwort auf die Frage