php - 将工作服务器与 php 应用程序一起使用的指南

标签 php web-worker worker

<分区>

我已经构建了一个 PHP 应用程序,并且我了解到在调用 api 或执行耗时的操作时使用“worker”+ 队列服务器是最佳实践。

对教程的快速搜索已经枯竭。我已经使用 codeigniter 构建了我的应用程序,并且我确实对 facebook api 进行了各种调用 + 在我的应用程序中使用基于 php 的图像处理。我唯一想知道的是,如果我正在执行 api 调用或调整图像大小,并且用户通常不会关心在完成之前从我的服务器获取响应,队列服务器+工作人员如何帮助我。

什么情况适合使用 worker + queue server,是否有任何指南可以将这些包含在我的应用程序中?最近我在我的应用程序中包含了内存缓存,这非常简单。我只是用内存缓存处理程序包装了我的 sql 查询。

最佳答案

在您描述的示例(调整图像大小)中,您基本上在调整图像大小所需的时间内保持 Apache 连接打开。 Apache 进程很昂贵,为了使您的系统尽可能可扩展,您应该尽可能缩短 Web 请求/响应时间。 另一个想法是使用队列可以控制并发性。如果 100 多个用户同时上传图片以调整大小怎么办?您的服务器可以处理吗?如果您有一个工作(后端)服务器来处理这些请求,那么您将能够只允许执行 X 个并发作业。

同样适用于 Web 服务请求:不是让连接保持打开状态,而是基本上将 Web 服务调用的执行卸载到工作进程,这释放了 Apache 进程,并且您可以实现 AJAX 轮询机制检查后端服务器向 web 服务发出的请求是否完成。从长远来看,系统的扩展性会更好,用户通常不喜欢等待操作完成而没有关于操作位置的反馈。队列允许您异步执行任务并向访问者提供有关任务完成状态的反馈。

我通常使用 Zend Server 完整版(商业版)提供的 Zend Server 作业队列(http://devzone.zend.com/article/11907http://devzone.zend.com/article/11907)。然而,Gearman 在这方面也很出色,并且有一个 PHP 扩展:http://php.net/manual/en/book.gearman.php和一个例子:http://www.php.net/manual/en/gearmanclient.do.php .

希望这对您有所帮助。

--编辑--

@Casey,我开始添加评论,但意识到这个答案很快就会变得太长,所以我编辑了答案。我刚刚阅读了云控制文档,这是一项我不知道的服务。不过幸运的是,我已经广泛使用了 Codeigniter,所以我会尝试为您破解一个答案:

1- Cloudcontrol 的 worker 概念是从命令行启动 php 脚本。因此,您需要一种方法让 Codeigniter 接受从命令行触发脚本并将其分派(dispatch)给 Controller 。您可能希望将其限制为一个 Controller 。查看代码:http://pastebin.com/GZigWbT3 该文件的作用与 CI 的 index.php 文件的作用相同,只是它通过设置 $_REQUEST['SERVER_URI'] 模拟请求。请务必将该文件放在文档根目录之外,并相应地调整 $system_folder 变量。

2- 您的 Controller 文件夹中需要一个 Controller script.php,您将从中禁用 Web 请求。您可以执行以下操作:

<?php
class script extends CI_Controller {
    public function __construct() {
        if(php_sapi_name() !== 'cli') {
            show_404();
        }
        parent::__construct();
    }

    public function resizeImage($arg1, $arg2) {
        //Whatever logic to resize image, or library call to do so.
    }
}

3- 最后一部分是让您在 CI 中(在您的 system/application/libraries 文件夹中)开发一个包装器库,它将有效地包装 CloudController 的 worker 调用的功能

    public function _construct() {
        $ci = get_instance();

        //add check to make sure that the value is set in the configuration
        //Ideally since this is a library, pass the app_name in a setter to avoid creating a dependancy on the config object.
        //Somewhere in one of your config files add $config['app_name'] = 'YOUR_APP_NAME/YOUR_DEP_NAME';
        //where APP_NAME and DEP_NAME are cloud controller's app_name and dep_name
        $this->_app_name = $ci->config->item('app_name');

        //Also add: $config['utilities_script'] = 'path/to/utilities.php';
        //This is the script created in step 1
        $this->_utilities_script = $ci->config->item('utilities_script');
    }

    public function run() {
        $args = func_get_args();
        if(count($args) < 1 ) {
            //We expect at least one arg which would be the command name
            trigger_error('Run expects at least one argument', E_USER_ERROR);
        }

        $method = array_shift($args);

        //utilities.php is the file created in step 1
        $command = "cctrlapp " . $this->_app_name . " worker.add ".$this->_utilities_script;

        //Add arguments if any
        $command .= ' "'.implode(' ', $args).'"';

        //finally...
        exec($command);
    }
}

4- 现在,如果来自 Controller ,则从代码中您实际想要排队作业的任何地方:

$this->load->library('Worker');
//resizeImage will call the method resizeImage in the script controller.
$this->worker->run('resizeImage', $width, $height);

请注意:
1- 这可以进一步完善,这实际上是为了让您了解它是如何完成的
2- 由于我没有 cloudcontroller 帐户,我无法测试代码,因此可能需要调整。我在我的项目中使用的 utilities.phph 脚本,所以这个应该不错。
祝你好运!

关于php - 将工作服务器与 php 应用程序一起使用的指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7249929/

相关文章:

php - 如何在提交表单时隐藏表单并显示进度条

php - for()中的mysql_fetch_array函数出现错误

javascript - 将 WebSocket 放在 WebWorker 上有意义吗?

php - gearman doBackground() 不使用 php

node.js - Azure Function Node.js 无法为运行时启动新的语言工作线程 : node

JavaScript 无法通过 SharedWorker() 构造函数

php - APC 缓存可以在以 fastcgi/fcgid 运行的多个 PHP 进程之间共享吗?

将某些日期字符串转换为日期时间值时出现 PHP strtotime 错误

javascript - "Uncaught ReferenceError: window is not defined"p5.js 网络 worker

javascript - 在 Angular Worker 中导入模块?