在您描述的示例(图像大小调整)中,您基本上在调整图像大小所需的时间内保持 Apache 连接打开。Apache 进程很昂贵,为了使您的系统尽可能地具有可扩展性,您应该致力于使您的 Web 请求/响应尽可能短。另一个想法是使用队列可以控制并发。如果 100 多个用户同时上传一张图片以调整大小怎么办?你的服务器能处理吗?如果您有一个工作(后端)服务器来处理这些请求,那么您将能够只允许执行 X 个并发作业。
同样适用于 Web 服务请求:您基本上将 Web 服务调用的执行卸载到工作进程,这释放了一个 apache 进程,并且您可以实现一个 AJAX 轮询机制来检查是否后端服务器向 Web 服务发出的请求已完成。从长远来看,系统会更好地扩展,用户通常不喜欢等待操作完成而没有反馈它在哪里。排队允许您异步执行任务并向访问者提供有关任务完成状态的反馈。
我通常使用 Zend Server 完整版(商业版)提供的 Zend Server 的作业队列(http://devzone.zend.com/article/11907和http://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 接受从命令行触发脚本并将其分派到控制器。您可能希望将其限制为一个控制器。见代码: http: //pastebin.com/GZigWbT3
这个文件本质上和 CI 的 index.php 文件一样,除了它通过设置模拟请求$_REQUEST['SERVER_URI']
。请务必将该文件放在文档根目录之外,并$system_folder
相应地调整变量。
2- 您需要在您的控制器文件夹中添加一个控制器 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 中开发一个包装器库(在您的系统/应用程序/库文件夹中),它将有效地包装 CloudController 的工作调用的功能
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-现在从代码中您实际想要排队作业的任何位置(如果来自控制器):
$this->load->library('Worker');
//resizeImage will call the method resizeImage in the script controller.
$this->worker->run('resizeImage', $width, $height);
请注意:
1- 这可以进一步完善,它实际上是为了让您了解如何完成
2- 由于我没有 cloudcontroller 帐户,我无法测试代码,因此可能需要调整。我在项目中使用的 utility.phph 脚本,所以这个应该不错。
祝你好运!