【问题标题】:send data to php background thread script将数据发送到php后台线程脚本
【发布时间】:2015-03-15 06:59:14
【问题描述】:

有时我有一个很长的任务,我不希望当前的 PHP 线程等待,我已经完成了类似以下的操作。在会话中传递数据有点笨拙,但似乎可行。

我有另一个类似的应用程序,除了 file1.php 没有被用户的客户端访问,而是被另一台服务器访问,并且用户只访问另一台服务器。因此,file1.php 中的session_start() 将无法使用会话 cookie,并且必须为每次出现创建单独的会话文件。

还有哪些其他选项可以将数据传递到后台工作脚本?我没有传递大量数据,但它仍然会是 1kb 左右。

file1.php

session_start();
$_SESSION['_xfr']=$_POST;
$status=exec($'/usr/bin/php -q /path/to/my/worker.php'.' '.session_id().' >/dev/null &');
echo('Tell client we are done.');

file2.php

session_id($argv[1]);//Set by parent
session_start();
$data=$_SESSION['_xfr'];
//Do some task

【问题讨论】:

    标签: php multithreading


    【解决方案1】:

    您可以通过 args 将数据发送为 json:

    file1.php

    $status=exec($'/usr/bin/php -q /path/to/my/worker.php'.' '.json_encode($_POST).' >/dev/null &');
    echo('Tell client we are done.');
    

    file2.php

    $data=json_decode($argv[1]);
    //Do some task
    

    【讨论】:

    【解决方案2】:

    如果目标是向客户端返回状态并继续处理,您可能会发现简单地执行类似操作会更容易。

    public function closeConnection($body, $responseCode){
        // Cause we are clever and don't want the rest of the script to be bound by a timeout.
        // Set to zero so no time limit is imposed from here on out.
        set_time_limit(0);
    
        // Client disconnect should NOT abort our script execution
        ignore_user_abort(true);
    
        // Clean (erase) the output buffer and turn off output buffering
        // in case there was anything up in there to begin with.
        ob_end_clean();
    
        // Turn on output buffering, because ... we just turned it off ...
        // if it was on.
        ob_start();
    
        echo $body;
    
        // Return the length of the output buffer
        $size = ob_get_length();
    
        // send headers to tell the browser to close the connection
        // remember, the headers must be called prior to any actual
        // input being sent via our flush(es) below.
        header("Connection: close\r\n");
        header("Content-Encoding: none\r\n");
        header("Content-Length: $size");
    
        // Set the HTTP response code
        // this is only available in PHP 5.4.0 or greater
        http_response_code($responseCode);
    
        // Flush (send) the output buffer and turn off output buffering
        ob_end_flush();
    
        // Flush (send) the output buffer
        // This looks like overkill, but trust me. I know, you really don't need this
        // unless you do need it, in which case, you will be glad you had it!
        @ob_flush();
    
        // Flush system output buffer
        // I know, more over kill looking stuff, but this
        // Flushes the system write buffers of PHP and whatever backend PHP is using
        // (CGI, a web server, etc). This attempts to push current output all the way
        // to the browser with a few caveats.
        flush();
    }
    

    否则,您的选项仅限于 HTTP 服务器中的多线程,并且在您以另一种方式执行时通过 exec 进行脱壳......但是,您可能想要 nohup 命令而不是简单地运行它作为一个孩子当前线程的进程。

    【讨论】:

    • 这就是目标。我需要测试一下。谢谢
    • 我认为这是最好的解决方案,并希望我能多次投票。然而,另一个答案回答了我的具体问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2012-06-15
    • 2012-01-06
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多