fork不';虽然我检查了一下,但还是没用;s已安装并在我的服务器上运行


fork doesn't work although I checked it's installed and running on my server

我在Ubuntu服务器上安装了fork(使用PHP Apache Codeigner),并检查它是否使用var_dump (extension_loaded('pcntl'));工作,得到了"true"输出(如何检查PCNTL模块是否存在)。

我有这个代码:

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";
    // Create   fork
    $pid = pcntl_fork();
    if(!$pid){
        for ($i=0; $i < $keyword_count ; $i++) { 
            // Inserts the inputs to the "keywords" table
            $this->kas_model->insert_keyword($keyword[$i], $prod, $country);
            // Gets relevant IDs for the inserted prod and keyword
            $last_inserted_key = $this->kas_model->get_last_rec('keywords');
            $keyword_id        = $last_inserted_key[0]->key_id;
            $prod_id           = $last_inserted_key[0]->key_prod;
            $prod_id_query     = $this->kas_model->get_prod_row_by_id($prod_id);
            $prod_id_a  = $prod_id_query[0]->prod_a_id;
            $prod_id_b  = $prod_id_query[0]->prod_b_id; 
            // Run the keyword query (on API) for today on each one of the keys and insert to DB aslong that the ID isn't 0.  
            if ( ($prod_id_a != 0) || ( !empty($prod_id_a) ) ) {
                $a_tdr = $this->get_var1_a_by_id_and_kw( $prod_id_a, $keyword[$i], $country);
            } else {
                $a_tdr['var1'] = 0;
                $a_tdr['var2'] = 0;
                $a_tdr['var3'] = 0;
            }
            if ( ($prod_id_b != 0) || ( !empty($prod_id_b) ) ) {
                $b_tdr = $this->get_var1_b_by_id_and_kw($prod_id_b, $keyword[$i], $country);
            } else {
                $b_tdr['var1'] = 0;
                $b_tdr['var2'] = 0;
                $b_tdr['var3'] = 0;     
            }
            $this->kas_model->insert_new_key_to_db($keyword_id, $a_tdr['var1'], $b_tdr['var1'], $a_tdr['var2'], $b_tdr['var2'], $a_tdr['var3'], $b_tdr['var3']);
        }
        exit($i);
    }

    // we are the parent (main), check child's (optional)
    while(pcntl_waitpid(0, $status) != -1){
        $status = pcntl_wexitstatus($status);
         // echo "Child $status completed'n";
        redirect('main/kas'); 
    }
    redirect('main/kas'); 
}

函数的作用是什么

该函数获取1个或多个关键字/s、一个国家var和一个产品ID,并在获取变量的外部缓慢API上运行查询(从同一控制器中运行其他函数),并将它们添加到数据库中。

问题:运行此函数时,如果我插入了很多关键字,页面会加载,加载,加载很长一段时间,直到完成,只有,然后-我可以继续浏览我的网站。所以我被告知要分叉,因为它只是发送一个在后台处理它的请求,所以每当点击提交按钮时,我都会被重定向到"main/kas"。

当前:我没有被重定向,但函数运行时没有任何错误。

有人告诉我,它应该可以工作,但它不工作——所以我猜我在代码中做错了什么(?),或者服务器中有其他东西不工作(??)。这是我第一次使用fork,所以我不知道如何使用in(在语法或服务器内)进行操作。

你能帮我调试这个问题吗?

http://www.electrictoolbox.com/mysql-connection-php-fork/

错误原因父进程和子进程共享相同的进程数据库连接。当第一个子进程退出时,它将断开与数据库的连接,这意味着所有连接都相同正在使用的进程将断开连接,从而导致任何进一步的查询失败。

解决方案解决方案是在分叉子进程,然后在每个子进程中建立新的连接过程第四个参数也应该传递给mysql_connect函数为"true",以确保建立新的链接;默认情况下共享现有连接是登录详细信息都是一样的。

问题是!

在子级中连接到服务器是否有效,以及是否有其他替代方法可以更好地做到这一点。