PHP +叉():如何运行在一个PHP code叉子 [英] PHP+fork(): How to run a fork in a PHP code

查看:169
本文介绍了PHP +叉():如何运行在一个PHP code叉子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上codeIgniter我的code - Ubuntu服务器。

I am running my code on CodeIgniter - Ubuntu Server.

我一直在研究了异步方式来运行的功能。我用codeIgniter。

I have been researching for async ways to run functions. I use CodeIgniter.

这是我的功能:

<?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使用变量与缓慢的API,并更新数据库。

The "foreach" uses variables with slow APIs, and updates the database.

我看到了使用叉子一些教程,但还没有完全了解它的语法部分。大多数我发现事情只是它是如何工作exmplanations(2个进程:亲子等),但不给怎么在code应用此的一个很好的解释。

I saw some tutorials using fork but haven't quite understood the syntax part of it. Most of the things I found were just exmplanations of how it works (2 processes: parent-child ect') but non-gave a good explanation of how applying this on the code.

有人能解释我是如何使用fork()语法工作?

Can someone explain how I work with the fork() syntax?

<一个href=\"http://stackoverflow.com/questions/3833013/continue-php-execution-after-sending-http-response\">Continue在发送HTTP响应

http://www.onlinetechtutorials.com/2014/06/how-to-run-php-$c$c-asynchronously.html

http://php.net/manual/en/function.pcntl- fork.php (更普遍)

从服务器端: https://www.youtube.com/watch?v = xVSPv-9x3gk

修改

我有没有得到它的权利?

Did I get it right?

<?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'); 

}
?>

这不会使用这个循环中产生任何问题?将它知道堆栈每个进程?

This won't cause any problem with using this inside a loop? will it know to stack each process?

推荐答案

您必须提什么操作系统您正在使用,因为 PCNTL扩展不可用在Windows平台上

你还必须知道,在Web服务器中在Linux / Unix启动的过程控制可以给你意想不到的效果,所以只有 CLI / CGI 推荐模式使用PCNTL

请仔细阅读本 PCNTL介绍

You must mention what's the operating system that you are using, because pcntl extensions are not available on Windows platform
Also you must be aware that activating process control on Linux/Unix within a web server can give you unexpected results, so only CLI/CGI mode is recommended to use PCNTL
Please read carefully this PCNTL Introduction

现在,您的code似乎是正确的,执行情况良好,但你必须使用此选项编译PHP - 启用 - PCNTL 来实现类似的功能PCNTL INT pcntl_fork(无效)否则你将得到

Now, your code seems to be correct and well implemented, but you must compile your PHP with this option --enable-pcntl to enable pcntl functions like int pcntl_fork(void) otherwise you will get

致命错误:调用未定义的函数pcntl_fork()

Fatal error: Call to undefined function pcntl_fork()

对于我来说,最好的解决方案来运行的功能/异步的方式方法是使用的pthreads ,如果​​你是这个建议有兴趣,我可以通过编辑添加的例子,以及如何在Windows或Linux平台上安装它在我的回应

For me, the best solution to run functions/methods in asynchronous way is to use pthreads, if you are interested by this advice i can edit my response by adding examples and how to install it on Windows or Linux platforms

这篇文章知道如何编译PHP

Read this article to know how to compile PHP

这篇关于PHP +叉():如何运行在一个PHP code叉子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆