php workerman定时任务的实现代码
网络编程 2021-07-04 23:24www.168986.cn编程入门
这篇文章主要介绍了php workerman定时任务的实现代码,长沙网络推广觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧
一、下载workerman
二、下载workerman/mysql
1、定时函数为匿名函数(闭包)
use \Workerman\Worker; use \Workerman\Lib\Timer; require_once './Workerman/Autoloader.php'; $task = new Worker(); // 开启多少个进程运行定时任务,注意多进程并发问题 $task->count = 1; $task->onWorkerStart = function($task) { // 每2.5秒执行一次 $time_interval = 2.5; Timer::add($time_interval, function() { echo "task run\n"; }); }; // 运行worker Worker::runAll();
2、定时函数为普通函数
require_once './Workerman/Autoloader.php'; use \Workerman\Worker; use \Workerman\Lib\Timer; // 普通的函数 function send_mail($to, $content) { echo "send mail ...\n"; } $task = new Worker(); $task->onWorkerStart = function($task) { $to = 'workerman@workerman.'; $content = 'hello workerman'; // 10秒后执行发送邮件任务,一个参数传递false,表示只运行一次 Timer::add(10, 'send_mail', array($to, $content), false); }; // 运行worker Worker::runAll();
3、定时函数为类的方法
require_once './Workerman/Autoloader.php'; use \Workerman\Worker; use \Workerman\Lib\Timer; class Mail { // 注意,回调函数属性必须是public public function send($to, $content) { echo "send mail ...\n"; } } $task = new Worker(); $task->onWorkerStart = function($task) { // 10秒后发送一次邮件 $mail = new Mail(); $to = 'workerman@workerman.'; $content = 'hello workerman'; Timer::add(10, array($mail, 'send'), array($to, $content), false); }; // 运行worker Worker::runAll();
4、定时函数为类方法(类内部使用定时器)
require_once './Workerman/Autoloader.php'; use \Workerman\Worker; use \Workerman\Lib\Timer; class Mail { // 注意,回调函数属性必须是public public function send($to, $content) { echo "send mail ...\n"; } public function sendLater($to, $content) { // 回调的方法属于当前的类,则回调数组第一个元素为$this Timer::add(10, array($this, 'send'), array($to, $content), false); } } $task = new Worker(); $task->onWorkerStart = function($task) { // 10秒后发送一次邮件 $mail = new Mail(); $to = 'workerman@workerman.'; $content = 'hello workerman'; $mail->sendLater($to, $content); }; // 运行worker Worker::runAll();
5、定时函数为类的静态方法
require_once './Workerman/Autoloader.php'; use \Workerman\Worker; use \Workerman\Lib\Timer; class Mail { // 注意这个是静态方法,回调函数属性也必须是public public static function send($to, $content) { echo "send mail ...\n"; } } $task = new Worker(); $task->onWorkerStart = function($task) { // 10秒后发送一次邮件 $to = 'workerman@workerman.'; $content = 'hello workerman'; // 定时调用类的静态方法 Timer::add(10, array('Mail', 'send'), array($to, $content), false); }; // 运行worker Worker::runAll();
6、定时函数为类的静态方法(带命名空间)
namespace Task; require_once './Workerman/Autoloader.php'; use \Workerman\Worker; use \Workerman\Lib\Timer; class Mail { // 注意这个是静态方法,回调函数属性也必须是public public static function send($to, $content) { echo "send mail ...\n"; } } $task = new Worker(); $task->onWorkerStart = function($task) { // 10秒后发送一次邮件 $to = 'workerman@workerman.'; $content = 'hello workerman'; // 定时调用带命名空间的类的静态方法 Timer::add(10, array('\Task\Mail', 'send'), array($to, $content), false); }; // 运行worker Worker::runAll();
7、定时器中销毁当前定时器(use闭包方式传递$timer_id)
use \Workerman\Worker; use \Workerman\Lib\Timer; require_once './Workerman/Autoloader.php'; $task = new Worker(); $task->onWorkerStart = function($task) { // 计数 $count = 1; // 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 & $timer_id = Timer::add(1, function()use(&$timer_id, &$count) { echo "Timer run $count\n"; // 运行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'; use \Workerman\Worker; use \Workerman\Lib\Timer; class Mail { public function send($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 = new Worker(); $task->onWorkerStart = function($task) { $mail = new Mail(); // 要想$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的进程上设置定时器。
use Workerman\Worker; use Workerman\Lib\Timer; require_once './Workerman/Autoloader.php'; $worker = new Worker(); $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用来写定时任务
<?php / Created by PhpStorm. User: Administrator Date: 2018/11/29 Time: 16:59 / use Workerman\Worker; use \Workerman\Lib\Timer; require_once "Workerman/Autoloader.php"; require_once "Connection.php"; $task = new Worker(); $task->onWorkerStart = function ($task) { global $db, $redis; $db = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'root', 'root', 'test'); $redis = new Redis(); $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); }); }; function curlGet($url = '', $options = []) { $ch = curl_init($url); curl_set($ch, CURLOPT_RETURNTRANSFER, 1); curl_set($ch, CURLOPT_TIMEOUT, 30); if (!empty($options)) { curl_set_array($ch, $options); } //https请求 不验证证书和host curl_set($ch, CURLOPT_SSL_VERIFYPEER, false); curl_set($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($ch); curl_close($ch); return $data; } function newGetOrderInfo($taobao, $orderId) { $taobao = urlencode($taobao); $url = "http://114.55.144.79/taobao/TradeFullinfoGetRequest.php?shop=$taobao&tid=$orderId"; $json = curlGet($url); return json_decode($json, true)['trade']; } Worker::runAll();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程