主頁(yè) > 知識(shí)庫(kù) > php workerman定時(shí)任務(wù)的實(shí)現(xiàn)代碼

php workerman定時(shí)任務(wù)的實(shí)現(xiàn)代碼

熱門標(biāo)簽:兼職做地圖標(biāo)注好賺錢嗎 海南外呼系統(tǒng)方案 蘇州外呼系統(tǒng)有效果嗎 智能電銷語(yǔ)音機(jī)器人資訊 地圖標(biāo)注怎么做商戶驗(yàn)證 山東電銷卡外呼系統(tǒng)原理是什么 打開百度地圖標(biāo)注 亳州企業(yè)外呼系統(tǒng) 400 電話 辦理

一、下載workerman

https://www.workerman.net/download

二、下載workerman/mysql

http://doc3.workerman.net/640201

1、定時(shí)函數(shù)為匿名函數(shù)(閉包)

use \Workerman\Worker;
use \Workerman\Lib\Timer;
require_once './Workerman/Autoloader.php';

$task = new Worker();
// 開啟多少個(gè)進(jìn)程運(yùn)行定時(shí)任務(wù),注意多進(jìn)程并發(fā)問(wèn)題
$task->count = 1;
$task->onWorkerStart = function($task)
{
  // 每2.5秒執(zhí)行一次
  $time_interval = 2.5;
  Timer::add($time_interval, function()
  {
    echo "task run\n";
  });
};

// 運(yùn)行worker
Worker::runAll();

2、定時(shí)函數(shù)為普通函數(shù)

require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

// 普通的函數(shù)
function send_mail($to, $content)
{
  echo "send mail ...\n";
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  // 10秒后執(zhí)行發(fā)送郵件任務(wù),最后一個(gè)參數(shù)傳遞false,表示只運(yùn)行一次
  Timer::add(10, 'send_mail', array($to, $content), false);
};

// 運(yùn)行worker
Worker::runAll();

3、定時(shí)函數(shù)為類的方法

require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

class Mail
{
  // 注意,回調(diào)函數(shù)屬性必須是public
  public function send($to, $content)
  {
    echo "send mail ...\n";
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // 10秒后發(fā)送一次郵件
  $mail = new Mail();
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  Timer::add(10, array($mail, 'send'), array($to, $content), false);
};

// 運(yùn)行worker
Worker::runAll();

4、定時(shí)函數(shù)為類方法(類內(nèi)部使用定時(shí)器)

require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

class Mail
{
  // 注意,回調(diào)函數(shù)屬性必須是public
  public function send($to, $content)
  {
    echo "send mail ...\n";
  }

  public function sendLater($to, $content)
  {
    // 回調(diào)的方法屬于當(dāng)前的類,則回調(diào)數(shù)組第一個(gè)元素為$this
    Timer::add(10, array($this, 'send'), array($to, $content), false);
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // 10秒后發(fā)送一次郵件
  $mail = new Mail();
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  $mail->sendLater($to, $content);
};

// 運(yùn)行worker
Worker::runAll();

5、定時(shí)函數(shù)為類的靜態(tài)方法

require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

class Mail
{
  // 注意這個(gè)是靜態(tài)方法,回調(diào)函數(shù)屬性也必須是public
  public static function send($to, $content)
  {
    echo "send mail ...\n";
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // 10秒后發(fā)送一次郵件
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  // 定時(shí)調(diào)用類的靜態(tài)方法
  Timer::add(10, array('Mail', 'send'), array($to, $content), false);
};

// 運(yùn)行worker
Worker::runAll();

6、定時(shí)函數(shù)為類的靜態(tài)方法(帶命名空間)

namespace Task;
require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

class Mail
{
  // 注意這個(gè)是靜態(tài)方法,回調(diào)函數(shù)屬性也必須是public
  public static function send($to, $content)
  {
    echo "send mail ...\n";
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // 10秒后發(fā)送一次郵件
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  // 定時(shí)調(diào)用帶命名空間的類的靜態(tài)方法
  Timer::add(10, array('\Task\Mail', 'send'), array($to, $content), false);
};

// 運(yùn)行worker
Worker::runAll();

7、定時(shí)器中銷毀當(dāng)前定時(shí)器(use閉包方式傳遞$timer_id)

use \Workerman\Worker;
use \Workerman\Lib\Timer;
require_once './Workerman/Autoloader.php';

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // 計(jì)數(shù)
  $count = 1;
  // 要想$timer_id能正確傳遞到回調(diào)函數(shù)內(nèi)部,$timer_id前面必須加地址符 
  $timer_id = Timer::add(1, function()use($timer_id, $count)
  {
    echo "Timer run $count\n";
    // 運(yùn)行10次后銷毀當(dāng)前定時(shí)器
    if($count++ >= 10)
    {
      echo "Timer::del($timer_id)\n";
      Timer::del($timer_id);
    }
  });
};

// 運(yùn)行worker
Worker::runAll();

8、定時(shí)器中銷毀當(dāng)前定時(shí)器(參數(shù)方式傳遞$timer_id)

require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

class Mail
{
  public function send($to, $content, $timer_id)
  {
    // 臨時(shí)給當(dāng)前對(duì)象添加一個(gè)count屬性,記錄定時(shí)器運(yùn)行次數(shù)
    $this->count = empty($this->count) ? 1 : $this->count;
    // 運(yùn)行10次后銷毀當(dāng)前定時(shí)器
    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能正確傳遞到回調(diào)函數(shù)內(nèi)部,$timer_id前面必須加地址符 
  $timer_id = Timer::add(1, array($mail, 'send'), array('to', 'content', $timer_id));
};

// 運(yùn)行worker
Worker::runAll();

9、只在指定進(jìn)程中設(shè)置定時(shí)器

一個(gè)worker實(shí)例有4個(gè)進(jìn)程,只在id編號(hào)為0的進(jìn)程上設(shè)置定時(shí)器。

use Workerman\Worker;
use Workerman\Lib\Timer;
require_once './Workerman/Autoloader.php';

$worker = new Worker();
$worker->count = 4;
$worker->onWorkerStart = function($worker)
{
  // 只在id編號(hào)為0的進(jìn)程上設(shè)置定時(shí)器,其它1、2、3號(hào)進(jìn)程不設(shè)置定時(shí)器
  if($worker->id === 0)
  {
    Timer::add(1, function(){
      echo "4個(gè)worker進(jìn)程,只在0號(hào)進(jìn)程設(shè)置定時(shí)器\n";
    });
  }
};
// 運(yùn)行worker
Worker::runAll();

示例

shipments.php用來(lái)寫定時(shí)任務(wù)

?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請(qǐng)求 不驗(yàn)證證書和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=$taobaotid=$orderId";
  $json  = curlGet($url);
  return json_decode($json, true)['trade'];
}

Worker::runAll();

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP定時(shí)執(zhí)行任務(wù)的3種方法詳解
  • PHP定時(shí)執(zhí)行計(jì)劃任務(wù)的多種方法小結(jié)
  • 如何使用純PHP實(shí)現(xiàn)定時(shí)器任務(wù)(Timer)
  • php定時(shí)計(jì)劃任務(wù)的實(shí)現(xiàn)方法詳解
  • php中定時(shí)計(jì)劃任務(wù)的實(shí)現(xiàn)原理
  • PHP中使用sleep函數(shù)實(shí)現(xiàn)定時(shí)任務(wù)實(shí)例分享
  • 詳解PHP執(zhí)行定時(shí)任務(wù)的實(shí)現(xiàn)思路
  • win7計(jì)劃任務(wù)定時(shí)執(zhí)行PHP腳本設(shè)置圖解
  • PHP實(shí)現(xiàn)定時(shí)執(zhí)行任務(wù)的方法
  • 詳解PHP實(shí)現(xiàn)執(zhí)行定時(shí)任務(wù)

標(biāo)簽:安康 呼倫貝爾 金華 溫州 清遠(yuǎn) 紹興 綏化 萊蕪

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php workerman定時(shí)任務(wù)的實(shí)現(xiàn)代碼》,本文關(guān)鍵詞  php,workerman,定時(shí),任務(wù),的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php workerman定時(shí)任務(wù)的實(shí)現(xiàn)代碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于php workerman定時(shí)任務(wù)的實(shí)現(xiàn)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章