Implementation code of php workerman timing task

  • 2021-11-10 09:13:21
  • OfStack

1. Download workerman

https://www.workerman.net/download

2. Download workerman/mysql

http://doc3.workerman.net/640201

1. The timing function is an anonymous function (closure)


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

$task = new Worker();
//  How many processes are started to run timed tasks, and pay attention to the concurrency of multiple processes 
$task->count = 1;
$task->onWorkerStart = function($task)
{
  //  Every 2.5 Second execution 1 Times 
  $time_interval = 2.5;
  Timer::add($time_interval, function()
  {
    echo "task run\n";
  });
};

//  Run worker
Worker::runAll();

2. The timing function is a common function


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

//  Ordinary function 
function send_mail($to, $content)
{
  echo "send mail ...\n";
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  // 10 The Send Mail task is executed after seconds, and finally 1 Parameter passing false That only runs 1 Times 
  Timer::add(10, 'send_mail', array($to, $content), false);
};

//  Run worker
Worker::runAll();

3. The method of timing function as class


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

class Mail
{
  //  Note that the callback function property must be public
  public function send($to, $content)
  {
    echo "send mail ...\n";
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // 10 Send in seconds 1 Secondary mail 
  $mail = new Mail();
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  Timer::add(10, array($mail, 'send'), array($to, $content), false);
};

//  Run worker
Worker::runAll();

4. The timing function is a class method (timer is used inside the class)


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

class Mail
{
  //  Note that the callback function property must be public
  public function send($to, $content)
  {
    echo "send mail ...\n";
  }

  public function sendLater($to, $content)
  {
    //  If the method of the callback belongs to the current class, the callback array 1 Elements are $this
    Timer::add(10, array($this, 'send'), array($to, $content), false);
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // 10 Send in seconds 1 Secondary mail 
  $mail = new Mail();
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  $mail->sendLater($to, $content);
};

//  Run worker
Worker::runAll();

5. Static method with timing function as class


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

class Mail
{
  //  Note that this is a static method, and the callback function property must also be public
  public static function send($to, $content)
  {
    echo "send mail ...\n";
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // 10 Send in seconds 1 Secondary mail 
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  //  Calling static methods of classes at regular intervals 
  Timer::add(10, array('Mail', 'send'), array($to, $content), false);
};

//  Run worker
Worker::runAll();

6. The timing function is a static method of class (with namespace)


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

class Mail
{
  //  Note that this is a static method, and the callback function property must also be public
  public static function send($to, $content)
  {
    echo "send mail ...\n";
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // 10 Send in seconds 1 Secondary mail 
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  //  Calling static methods of classes with namespaces at regular intervals 
  Timer::add(10, array('\Task\Mail', 'send'), array($to, $content), false);
};

//  Run worker
Worker::runAll();

7. Destroy the current timer in the timer (use closure mode passes $timer_id)


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

$task = new Worker();
$task->onWorkerStart = function($task)
{
  //  Count 
  $count = 1;
  //  If you want to $timer_id Can be correctly passed into the callback function, $timer_id You must be preceded by an address character  &
  $timer_id = Timer::add(1, function()use(&$timer_id, &$count)
  {
    echo "Timer run $count\n";
    //  Run 10 Destroy the current timer after the second time 
    if($count++ >= 10)
    {
      echo "Timer::del($timer_id)\n";
      Timer::del($timer_id);
    }
  });
};

//  Run worker
Worker::runAll();

8. Destroy the current timer in the timer (pass $timer_id in parameter mode)


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

class Mail
{
  public function send($to, $content, $timer_id)
  {
    //  Temporarily add to the current object 1 A count Property to record the number of times the timer runs 
    $this->count = empty($this->count) ? 1 : $this->count;
    //  Run 10 Destroy the current timer after the second time 
    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();
  //  If you want to $timer_id Can be correctly passed into the callback function, $timer_id You must be preceded by an address character  &
  $timer_id = Timer::add(1, array($mail, 'send'), array('to', 'content', &$timer_id));
};

//  Run worker
Worker::runAll();

9. Set the timer only in the specified process

1 worker instance has 4 processes, and the timer is set only on the process with id number 0.


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

$worker = new Worker();
$worker->count = 4;
$worker->onWorkerStart = function($worker)
{
  //  Only in id Number is 0 Set a timer on the process of, others 1 , 2 , 3 No timer is set for the # process 
  if($worker->id === 0)
  {
    Timer::add(1, function(){
      echo "4 A worker Process, only in 0 # process setting timer \n";
    });
  }
};
//  Run worker
Worker::runAll();

Example

shipments. php is used to write timing tasks


<?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 Request   Do not verify certificates and 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();


Related articles: