fork 不工作虽然我检查它安装和 运行 在我的服务器上
fork doesn't work although I checked it's installed and running on my server
我在我的 Ubuntu 服务器上安装了 fork(使用 PHP-Apache-Codeigniter),并使用 var_dump (extension_loaded('pcntl'));
检查它是否工作并得到 "true" 输出( How to check PCNTL module exists).
我有这个代码:
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 个或多个 keyword/s、一个国家/地区变量和一个产品 ID,并在外部缓慢运行查询 API 获取变量(从同一控制器中运行其他函数), 并将它们添加到数据库中。
问题: 当运行这个函数的时候,如果我插入很多关键字,页面加载,加载,加载,很长一段时间,直到完成,只有 然后 - 我可以继续浏览我的网站。所以我被告知要分叉它,因为它只是发送一个请求在后台处理它,所以每当点击提交按钮时,我都会被重定向到 "main/kas"。
目前:我没有被重定向,但函数运行没有任何错误。
我被告知它应该可以工作,但事实并非如此 - 所以我猜我在代码中做错了什么(?),或者服务器中的其他东西不工作(???) .这是我第一次使用 fork,所以我不太了解如何使用 in(语法或服务器内部)进行操作。
你能帮我调试问题吗?
http://www.electrictoolbox.com/mysql-connection-php-fork/
Reason for the error The parent and child processes all share the same
database connection. When the first child process exits it will
disconnect from the database, which means the same connection all
processes are using will be disconnected, causing any further queries
to fail.
The solution The solution is to disconnect from the database before
forking the sub processes and then establish a new connection in each
process. The fourth parameter also should be passed to the
mysql_connect function as "true" to ensure a new link is established;
the default is to share an existing connection is the login details
are the same.
问题来了!
连接到 child 中的服务器是否有效,是否有任何其他替代方法可以更好地做到这一点。
我在我的 Ubuntu 服务器上安装了 fork(使用 PHP-Apache-Codeigniter),并使用 var_dump (extension_loaded('pcntl'));
检查它是否工作并得到 "true" 输出( How to check PCNTL module exists).
我有这个代码:
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 个或多个 keyword/s、一个国家/地区变量和一个产品 ID,并在外部缓慢运行查询 API 获取变量(从同一控制器中运行其他函数), 并将它们添加到数据库中。
问题: 当运行这个函数的时候,如果我插入很多关键字,页面加载,加载,加载,很长一段时间,直到完成,只有 然后 - 我可以继续浏览我的网站。所以我被告知要分叉它,因为它只是发送一个请求在后台处理它,所以每当点击提交按钮时,我都会被重定向到 "main/kas"。
目前:我没有被重定向,但函数运行没有任何错误。
我被告知它应该可以工作,但事实并非如此 - 所以我猜我在代码中做错了什么(?),或者服务器中的其他东西不工作(???) .这是我第一次使用 fork,所以我不太了解如何使用 in(语法或服务器内部)进行操作。
你能帮我调试问题吗?
http://www.electrictoolbox.com/mysql-connection-php-fork/
Reason for the error The parent and child processes all share the same database connection. When the first child process exits it will disconnect from the database, which means the same connection all processes are using will be disconnected, causing any further queries to fail.
The solution The solution is to disconnect from the database before forking the sub processes and then establish a new connection in each process. The fourth parameter also should be passed to the mysql_connect function as "true" to ensure a new link is established; the default is to share an existing connection is the login details are the same.
问题来了!
连接到 child 中的服务器是否有效,是否有任何其他替代方法可以更好地做到这一点。