原标题:PHP与workerman实现定时任务
码农程序技术交流群
学习需要交流,技术互相提升,问题一起探讨,QQ群:748431051
内容正文
workerman是一个高性能的PHP socket 服务器框架,workerman基于PHP多进程以及libevent事件轮询库,PHP开发者只要实现一两个接口,便可以开发出自己的网络应用,例如Rpc服务、聊天室服务器、手机游戏服务器等。
workerman的目标是让PHP开发者更容易的开发出基于socket的高性能的应用服务,而不用去了解PHP socket以及PHP多进程细节。 workerman本身是一个PHP多进程服务器框架,具有PHP进程管理以及socket通信的模块,所以不依赖php-fpm、nginx或者apache等这些容器便可以独立运行
以下我们来重点来来workerman是如何实现定时任务的
一、下载workerman
https://www.workerman.net/download
二、下载workerman/mysql
http://doc3.workerman.net/640201
1、定时函数为匿名函数(闭包)
useWorkermanWorker;
useWorkermanLibTimer;
require_once'./Workerman/Autoloader.php';
$task = newWorker();
// 开启多少个进程运行定时任务,注意多进程并发问题
$task->count = 1;
$task->onWorkerStart = function($task)
{
// 每2.5秒执行一次
$time_interval = 2.5;
Timer::add($time_interval, function()
{
echo"task runn";
});
};
// 运行worker
Worker::runAll();
2、定时函数为普通函数
require_once'./Workerman/Autoloader.php';
useWorkermanWorker;
useWorkermanLibTimer;
// 普通的函数
functionsend_mail($to, $content)
{
echo"send mail ...n";
}
$task = newWorker();
$task->onWorkerStart = function($task)
{
$to = 'workerman@workerman.net';
$content = 'hello workerman';
// 10秒后执行发送邮件任务,最后一个参数传递false,表示只运行一次
Timer::add( 10, 'send_mail', array($to, $content), false);
};
// 运行worker
Worker::runAll();
3、定时函数为类的方法
require_once'./Workerman/Autoloader.php';
useWorkermanWorker;
useWorkermanLibTimer;
classMail
{
// 注意,回调函数属性必须是public
publicfunctionsend($to, $content)
{
echo"send mail ...n";
}
}
$task = newWorker();
$task->onWorkerStart = function($task)
{
// 10秒后发送一次邮件
$mail = newMail();
$to = 'workerman@workerman.net';
$content = 'hello workerman';
Timer::add( 10, array($mail, 'send'), array($to, $content), false);
};
// 运行worker
Worker::runAll();
4、定时函数为类方法(类内部使用定时器)
require_once'./Workerman/Autoloader.php';
useWorkermanWorker;
useWorkermanLibTimer;
classMail
{
// 注意,回调函数属性必须是public
publicfunctionsend($to, $content)
{
echo"send mail ...n";
}
publicfunctionsendLater($to, $content)
{
// 回调的方法属于当前的类,则回调数组第一个元素为$this
Timer::add( 10, array( $this, 'send'), array($to, $content), false);
}
}
$task = newWorker();
$task->onWorkerStart = function($task)
{
// 10秒后发送一次邮件
$mail = newMail();
$to = 'workerman@workerman.net';
$content = 'hello workerman';
$mail->sendLater($to, $content);
};
// 运行worker
Worker::runAll();
5、定时函数为类的静态方法
require_once'./Workerman/Autoloader.php';
useWorkermanWorker;
useWorkermanLibTimer;
classMail
{
// 注意这个是静态方法,回调函数属性也必须是public
publicstaticfunctionsend($to, $content)
{
echo"send mail ...n";
}
}
$task = newWorker();
$task->onWorkerStart = function($task)
{
// 10秒后发送一次邮件
$to = 'workerman@workerman.net';
$content = 'hello workerman';
// 定时调用类的静态方法
Timer::add( 10, array( 'Mail', 'send'), array($to, $content), false);
};
// 运行worker
Worker::runAll();
6、定时函数为类的静态方法(带命名空间)
namespaceTask;
require_once'./Workerman/Autoloader.php';
useWorkermanWorker;
useWorkermanLibTimer;
classMail
{
// 注意这个是静态方法,回调函数属性也必须是public
publicstaticfunctionsend($to, $content)
{
echo"send mail ...n";
}
}
$task = newWorker();
$task->onWorkerStart = function($task)
{
// 10秒后发送一次邮件
$to = 'workerman@workerman.net';
$content = 'hello workerman';
// 定时调用带命名空间的类的静态方法
Timer::add( 10, array( 'TaskMail', 'send'), array($to, $content), false);
};
// 运行worker
Worker::runAll();
7、定时器中销毁当前定时器(use闭包方式传递$timer_id)
useWorkermanWorker;
useWorkermanLibTimer;
require_once'./Workerman/Autoloader.php';
$task = newWorker();
$task->onWorkerStart = function($task)
{
// 计数
$count = 1;
// 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 &
$timer_id = Timer::add( 1, function()use(&$timer_id, &$count)
{
echo"Timer run $countn";
// 运行10次后销毁当前定时器
if($count++ >= 10)
{
echo"Timer::del($timer_id)n";
Timer::del($timer_id);
}
});
};
// 运行worker
Worker::runAll();
8、定时器中销毁当前定时器(参数方式传递$timer_id)
require_once'./Workerman/Autoloader.php';
useWorkermanWorker;
useWorkermanLibTimer;
classMail
{
publicfunctionsend($to, $content, $timer_id)
{
// 临时给当前对象添加一个count属性,记录定时器运行次数
$this->count = empty( $this->count) ? 1: $this->count;
// 运行10次后销毁当前定时器
echo"send mail {$this->count}...n";
if( $this->count++ >= 10)
{
echo"Timer::del($timer_id)n";
Timer::del($timer_id);
}
}
}
$task = newWorker();
$task->onWorkerStart = function($task)
{
$mail = newMail();
// 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 &
$timer_id = Timer::add( 1, array($mail, 'send'), array( 'to', 'content', &$timer_id));
};
// 运行worker
Worker::runAll();
9、只在指定进程中设置定时器
一个worker实例有4个进程,只在id编号为0的进程上设置定时器。
useWorkermanWorker;
useWorkermanLibTimer;
require_once'./Workerman/Autoloader.php';
$worker = newWorker();
$worker->count = 4;
$worker->onWorkerStart = function($worker)
{
// 只在id编号为0的进程上设置定时器,其它1、2、3号进程不设置定时器
if($worker->id === 0)
{
Timer::add( 1, function(){
echo"4个worker进程,只在0号进程设置定时器n";
});
}
};
// 运行worker
Worker::runAll();
看看完整的示例
shipments.php用来写定时任务
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/11/29
* Time: 16:59
*/
useWorkermanWorker;
useWorkermanLibTimer;
require_once"Workerman/Autoloader.php";
require_once"Connection.php";
$task = newWorker();
$task->onWorkerStart = function($task){
global$db, $redis;
$db = newWorkermanMySQLConnection('127.0.0.1', '3306', 'root', 'root', 'test');
$redis = newRedis();
$redis->connect('127.0.0.1', 6379);
$redis->auth("qqq123123.");
$time_interval = 0.1;
Timer::add($time_interval, function(){
global$db, $redis;
$insert['name'] = 123;
$db->insert('shipments')->cols($insert)->query();
// sleep(100);
});
};
functioncurlGet($url = '', $options = [])
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if(!empty($options)) {
curl_setopt_array($ch, $options);
}
//https请求 不验证证书和host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return$data;
}
functionnewGetOrderInfo($taobao, $orderId)
{
$taobao = urlencode($taobao);
$url = "http://114.55.144.79/taobao/TradeFullinfoGetRequest.php?shop=$taobao&tid=$orderId";
$json = curlGet($url);
returnjson_decode($json, true)['trade'];
}
Worker::runAll();返回搜狐,查看更多
责任编辑: