0

在 Kohana 3 中如何可能在控制器的 action_index 方法中并使用类似此代码的内容

 exec("php index.php --uri=home/other_method");

...在后台运行“other_method”?我在“home/index”中,想将“home/other_method”作为后台任务调用。我不想等待网页响应,“other_method”最多可能需要 3 分钟才能完成。

我不想使用队列,我想在没有 Minion 的情况下这样做。

这可能吗?无论我尝试什么,我都无法让“other_method”做出回应。我可以直接访问它,甚至可以使用服务器上的 CRON 作业来调用我的 exec() 代码。

为什么我不能在 action_index 中通过 exec() 访问它?是否有另一种方法可以使用 kohana 线程调用“other_method”,以便我可以继续使用 action_index?

(另外,我在 Mac 上使用 MAMP 编码。也许它与环境有关?)

4

1 回答 1

1
  • 在控制器的顶部home/other_method为您的脚本不要停止设置:

    ignore_user_abort(true);
    set_time_limit(0);
    
  • 创建流上下文,将其超时配置为短时间段,并且在达到超时时不产生异常:

    $context = stream_create_context(array(
        'http'=>array(
            'timeout' => 1.0, // Set timeout to 1 second
            'ignore_errors' => true // Don't prouce Exception
        )
    ));
    
  • (假设您的home/other_methodURL 是http://localhost/other_method)使用创建的上下文调用您的 URL:

    $kick = file_get_contents(`'http://localhost/other_method'`, false, $context);
    

达到超时后,file_get_contents()停止返回空字符串,但您的请求将在后台继续直到结束。

小心调用'http://localhost/other_method'多次,所有这些请求将同时运行。

您可以使用带有 CURL的 Kohana 的Request_Client_External类创建类似的东西。

于 2013-11-05T14:47:55.713 回答