CREATE TABLE`queue` (
id int(11) NOT NULL auto_increment primarykey,
taskphp varchar(128) NOT NULL default '',
param text not null default '',
status tinyint not null default 0,
ctime timestamp NOT NULL default CURRENT_TIMESTAMP,
KEY (ctime)
) ENGINE=InnoDBDEFAULT CHARSET=utf8;字段解释:<?php
/**
*
* 任务队列实现
*
*/
include_once('db.php');
class Queue
{
/**
* 把任务扔到队列
*
* @param string $taskphp 执行任务的程序
* @param string $param 执行任务程序所用的参数
* 例如,群发消息加入队列:
* $arr = array(
* "uid" => 4,//发信息的人的UID
* "uids" => array(6,234,34,67,7888,2355), //接收信息的人的UID
* "content" => 'xxxxx',//信息内容
* );
* $cqueue = new Queue();
* $cqueue->add("/app/send_msg.php", serialize($arr));
*
*/
public function add($taskphp,$param)
{
$taskphp = mysql_real_escape_string($taskphp);
//$param = mysql_real_escape_string($param);
$param = $param;
$sql = "insert into queue (taskphp, param) values('".$taskphp."', '".$param."')";
$re = execute($sql);
if ($re)
{
$pid = mysql_insert_id();
return $pid;
}
else
{
return false;
}
}
/**
* 读取任务队列
*
* @param string $limit 一次取多少条
*/
public function getQueueTask($limit = 1000)
{
$limit = (int)$limit;
$sql = "select id, taskphp, param from queue where status = 0 order by id asc";
$re = query($sql);
return $re;
}
/**
* 更新任务状态
*
* @param string $limit 一次取多少条
*/
public function updateTaskByID($id)
{
$id = (int)$id;
$mtime = time();
$sql = "update queue set status =1, mtime = ".$mtime." where id = ".$id;
$re = execute($sql);
return $re;
}
public static function a2s($arr)
{
$str = "";
foreach ($arr as $key => $value)
{
if (is_array($value))
{
foreach ($value as $value2)
{
$str .= urlencode($key) . "[]=" . urlencode($value2) . "&";
}
}
else
{
$str .= urlencode($key) . "=" . urlencode($value) . "&";
}
}
return $str;
}
public static function s2a($str)
{
$arr = array();
parse_str($str, $arr);
return $arr;
}
}
?>1:加入队列接口$phpcmd = exec("which php"); //查找到php安装位置
$cqueue = new Queue();
$tasks = $cqueue->getQueueTask(200);
foreach ($tasks as $t)
{
$taskphp = $t['taskphp'];
$param = $t['param'];
$job = $phpcmd . " " . escapeshellarg($taskphp) . " " . escapeshellarg($param);
system($job);
}七、具体任务的业务实现$para = $argv[1];
$arr = unserialize($para);
$cmessage = new Message();
foreach($arr['uids'] as $touid)
{
$cmessage->send($arr['uid'], $touid, $arr['content']);
}八、服务器部署一:配置crontabif ($minute % 5 == 0)
{
if(chdir($site_dir."app/")) {
$cmd = "$phpcmd do_queue.php > do_queue.log &";
echo '[' , $ymd , ' ' , $hour , ':' , $minute , '] ' , $cmd , "n";
system($cmd);
}
}十、开启多进程并发执行队列if ($minute % 5 == 0)
{
if(chdir($site_dir."app/")) {
$cmd = "$phpcmd do_queue.php > do_queue.log &";
echo '[' , $ymd , ' ' , $hour , ':' , $minute , '] ' , $cmd , "n";
system($cmd);
}
}修改后:if ($minute % 5 == 0)
{
for ($i=0; $i < 10; $i++) {
$cmd = "$phpcmd doQueue.php 10 $i>> doQueueMission".date('Y-m-d').".log ";
echo date("Y-m-d H:i:s") . "t : " .$cmd."n";
system($cmd);
}
}//每次进行10个进程,$i来区分是当前的进程标示$phpcmd = 'D:workwampbinphpphp5.3.10php ';
$cqueue = new Queue();
$tasks = $cqueue->getQueueTask(200);修改后:$phpcmd = 'D:workwampbinphpphp5.3.10php ';
$total=$argv[1];
$i=$argb[2];
$cqueue = new Queue();
$tasks = $cqueue->getQueueTask($total,$i,200);3:取队列接口的修改public function getQueueTask($limit = 1000)
{
$limit = (int)$limit;
$sql = "select id, taskphp, param from queue where status = 0 order by id asc";
$re = query($sql);
return $re;
}修改后:public function getQueueTask($total,$i,$limit = 1000)
{
$limit = (int)$limit;
$sql = "select id, taskphp, param from queue where status = 0 and id%$total=$i order by id asc";
$re = query($sql);
return $re;
}4:需要关注服务器压力ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。