php workerman定時任務的實現代碼

這篇文章主要介紹了php workerman定時任務的實現代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、下載workerman

https://www.workerman.net/download

二、下載workerman/mysql

http://doc3.workerman.net/640201

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 = '[email protected]';
  $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 = '[email protected]';
  $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 = '[email protected]';
  $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 = '[email protected]';
  $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 = '[email protected]';
  $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_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;
}

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();

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章