php使用pthreads的实用例子
class Thread_request extends Thread { public static $thread_tasks; protected $processor; public $url; public $params; public $model; public $view; public $response; public $last_url; public $last_params; public $last_model; public $last_view; public $last_error = ''; public $last_errno = 0; public $status = ''; public $completed = false;
//$processor是一个远程调用其他接口类的对象 public function __construct($processor = null, $auto_start = true) { if ($processor !== null ) { $this->processor = $processor; } if ($auto_start) { $this->start(); } } public function url($url = null) { if ($url === null ) { return $this->url; } $this->url = $url; return $this; } public function params($params = null) { if ($params === null ) { return $this->params; } $this->params = $params; return $this; } public function model($model = null) { if ($model === null ) { return $this->model; } $this->model = $model; return $this; } public function run() { $this->synchronized(function($self) { $counting = 0; do { if (!$self->isWaiting()) { $self->wait(); } if ($self->completed == true) { break; } if ($self->status == '') { continue; } $self->response = $self->processor->post($self->url, $self->params); $self->last_url = $self->url; $self->last_params = $self->params; $self->last_model = $self->model; $self->last_view = $self->view; $self->status = ''; $counting++; } while ($self->current_task <= self::$thread_tasks && $counting <= self::$thread_tasks); }, $this); } }
调用过程
/** * Pipe 模式 */ public function index_pipe() { // 输出框架页面 $this->render('index', array(), false); // 接口队列 $request_list = array( // 入驻企业 0 => array( 'url' => 'http://api.X.com/suplier_enterprise.ld', 'params' => array(), 'model' => '', 'view' => 'new_join' ), // 推荐品牌 9 => array( 'url' => 'goodsBrand/queryIndexGoodsBrand.action', 'params' => array(), 'model' => '', 'view' => 'recom_brand' ), // 点击排行 2 => array( 'url' => 'goodshow/clickTop.action', 'params' => array(), 'model' => '', 'view' => 'rank_views' ), // 销量排行 3 => array( 'url' => 'goodshow/totalTop.action', 'params' => array(), 'model' => '', 'view' => 'rank_sales' ), // 交易动态 4 => array( 'url' => 'order/dealDynamic.action', 'params' => array(), 'model' => '', 'view' => 'trade_info' ), // 推荐品种 6 => array( 'url' => 'goodssuggest/goodssuggestInfo.action', 'params' => array( 'query_type' => 1, 'gsc_name' => '新品推荐', 'enterpriseSuppliers' => $this->supplier_enterprise ), 'model' => '', 'view' => 'recom_breed_list' ), // 最新促销 7 => array( 'url' => 'goodspromotions/goodsPromotionsInfo.action', 'params' => array( 'query_type' => 1, 'gpc_id' => 1, 'page' => array( 'startNum' => 0, 'pageSize' => 5 ), 'enterpriseSuppliers' => $this->supplier_enterprise ), 'model' => '', 'view' => 'promotion_list' ), // 首页广告位以及分类展示所有集合 8 => array( 'url' => 'goodshow/homeAd.action', 'params' => array('enterpriseSuppliers' => $this->supplier_enterprise), 'model' => '', 'view' => 'index_ads' ), // 系统公告 10 => array( 'url' => config_item('news_url') . 'api.php?op=news', 'params' => array('action' => 'notice', 'notice_num' => 5), 'model' => '', 'view' => 'site_announ' ), // 获取一条最新带缩略图的新闻资讯 11 => array( 'url' => config_item('news_url') . 'api.php?op=news', 'params' => array('action' => 'img'), 'model' => '', 'view' => 'picture_news' ), // 最新资讯 12 => array( 'url' => config_item('news_url') . 'api.php?op=news', 'params' => array('news_num' => 12), 'model' => '', 'view' => 'text_news' ), // 获取一条最新带缩略图的招商资讯 13 => array( 'url' => config_item('zs_url') . 'news/b2b_business_thumb', 'params' => array(), 'model' => '', 'view' => 'picture_merchants' ), // 热门招商 14 => array( 'url' => config_item('zs_url') . 'news/b2b_business', 'params' => array('bus_num' => 12), 'model' => '', 'view' => 'text_merchants' ), // 获取商品分类 23 => array( 'url' => 'goodsCate/goodsCateList.action', 'params' => array(), 'model' => '', 'view' => 'cate_list' ), // 获取搜索热词 24 => array( 'url' => 'goodshow/getSearchHotWords.action', 'params' => array('rowNum' => 5), 'model' => '', 'view' => 'hot_keywords' ), ); // 加载多线程处理类 include_once dirname(dirname(__FILE__)) . '/libraries/thread_request.php'; // 接口请求队列 $api_queues = Thread_request::$thread_tasks = count($request_list); // 创建线程池 $pools[] = new Thread_request($this->mph_sdk); $pools[] = new Thread_request($this->mph_sdk); $pools[] = new Thread_request($this->mph_sdk); $pools[] = new Thread_request($this->mph_sdk); $pools[] = new Thread_request($this->mph_sdk); // 派发任务给线程 for ($i = 1; $i < $api_queues + 1; $i++) { $current_api = array_shift($request_list); while (true) { // 遍历线程池, 为空闲线程分配请求任务 foreach ($pools as $worker) { // 线程处于空闲等待中 if ($worker->isWaiting()) { $worker->synchronized(function($self, $thread, $api_params, $current_task) { $thread->status = 1; $thread->url = $api_params['url']; $thread->params = $api_params['params']; $thread->model = $api_params['model']; $thread->view = $api_params['view']; $thread->current_task = $current_task; $thread->notify(); // 上一次处理完毕的响应信息 if ($thread->response) { // 输出 Pagelet $self->render($thread->last_view, $thread->response, $thread->last_url); } }, $this, $worker, $current_api, $i); break 2; } } } } // 等待所有线程运行结束 while (count($pools)) { // 遍历检查线程组运行结束 foreach ($pools as $key => $thread) { if ($thread->status == '' || $thread->isWaiting()) { $thread->synchronized(function($self, $thread) { // 输出 Pagelet $self->render($thread->last_view, $thread->response, $thread->last_url); if ($thread->isWaiting()) { $thread->completed = true; $thread->notify(); } }, $this, $thread); // 销毁线程 $thread = null; unset($pools[$key]); } } } // 所有线程执行完毕 $this->render('page_chain', array(), false); } //类似facebook的管道输出,先输出框架,然后线程调用接口之后通过输出js,把内容插入相应的地方 public function render($view = '', $data = array(), $is_pagelet = true, $method = 'html') { $api_result = isset($data['result']) ? $data['result'] : $data; if (!is_array($api_result)) { $api_result = json_decode($api_result, true); } unset($data['code'], $data['message'], $data['result']); if (!isset($data[$view])) { $data[$view] = $api_result; } if ($view == 'recom_breed_list' && !isset($data['l_id'])) { $data['l_id'] = '新品推荐'; } if ($view == 'promotion_list' && !isset($data['l_id'])) { $data['l_id'] = '1'; } if ($view == 'index_ads') { // 获取橱窗集合 $showcase_info = isset($api_result['syChuChuangList']) ? $api_result['syChuChuangList'] : array(); $this->render('showcase_info', array('showcase_info' => $showcase_info)); // 获取感兴趣产品集合 $interested_info = isset($api_result['syHdt']) ? $api_result['syHdt'] : array(); $this->render('interested_info', array('interested_info' => $interested_info)); // 首页焦点图 $index_focus_silde = isset($api_result['syJdtList']) ? $api_result['syJdtList'] : array(); $this->render('index_focus_silde', array('index_focus_silde' => $index_focus_silde)); // 首页推荐品种图片 $recom_breed_tit = isset($api_result['syTjpzList']) ? $api_result['syTjpzList'] : array(); $this->render('recom_breed_tit', array('recom_breed_tit' => $recom_breed_tit)); // 首页最新促销图片 $promotion_tit = isset($api_result['syZxcxList']) ? $api_result['syZxcxList'] : array(); $this->render('promotion_tit', array('promotion_tit' => $promotion_tit)); // 首页中间广告位 $index_center_ads = (isset($api_result['centerAds'])) ? $api_result['centerAds'] : array(); $this->render('index_center_ads', array('index_center_ads' => $index_center_ads)); // 首页右下角广告 $index_bottom_right_ads = (isset($api_result['bottomAds'][0])) ? $api_result['bottomAds'][0] : ''; $this->render('index_bottom_right_ads', array('index_bottom_right_ads' => $index_bottom_right_ads)); return; } // Pagelet 模版 $pagelet_html = $this->load->view("index/pagelet/$view", $data, true); if ($is_pagelet) { $pagelet_html = addslashes($pagelet_html); $pagelet_html = str_replace(PHP_EOL, '\\' . PHP_EOL, $pagelet_html); $pagelet_html = '<script type="text/javascript">load_pagelet(\'' . $view . '\', \'' . $pagelet_html . '\', \'' . $method . '\');</script>'; } // 填充字符串 if (strlen($pagelet_html) < 1024) { $pagelet_html = str_pad($pagelet_html, 1024); } echo $pagelet_html; flush(); }
posted on 2014-04-03 17:26 kudosharry 阅读(582) 评论(0) 编辑 收藏 举报