在您描述的示例(图像大小调整)中,您基本上保持Apache连接在调整图像大小所需的时间内保持打开状态. Apache流程非常昂贵,为了使您的系统尽可能具有可扩展性,您应该尽可能缩短Web请求/响应.
另一个想法是,使用队列,您可以控制并发.如果100个用户同时上传图像以进行调整大小怎么办?你的服务器可以处理吗?如果您有一个工作(后端)服务器来处理这些请求,那么您将能够仅允许执行X个并发作业.
同样适用于Web服务请求:您没有保持打开的连接,而是基本上将Web服务调用的执行卸载到工作进程,这释放了一个apache进程,并且您可以实现一个AJAX轮询机制来检查是否存在请求发送到Web服务的后端服务器已完成.从长远来看,系统将更好地扩展,用户通常不喜欢等待操作完成而没有反馈它在哪里.排队允许您异步执行任务并向访问者提供有关任务完成状态的反馈.
希望这可以帮助.
– 编辑 –
@Casey,我开始添加评论,但意识到这很快就会变得太长,所以我编辑了答案.我刚刚阅读了云控制的文档,这是一项我不知道的服务.幸运的是,我已经广泛使用了Codeigniter,所以我会试着为你解答一个答案:
1- Cloudcontrol的工作者概念是从命令行启动php脚本.因此,您需要一种方法让Codeigniter接受从命令行触发脚本并将其分派到控制器.您可能希望将其限制为一个控制器.请参阅代码:http://pastebin.com/GZigWbT3
该文件实质上是CI的index.php文件所做的,除了它通过设置$_REQUEST [‘SERVER_URI’]来模拟请求.确保将该文件放在文档根目录之外,并相应地调整$system_folder变量.
2-您需要控制器文件夹中的控制器script.php,您将从中禁用Web请求.您可以采取以下措施:
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);
Pelase注意到:1-这可以进一步完善,它真的能让你了解它是如何完成的2-由于我没有cloudcontroller帐户,我无法测试代码,因此可能需要调整.我在我的项目中使用的utilities.phph脚本,所以这个应该是好的.祝好运!