Swoft2.x white study notes (c) --- Task, coroutine

The introduction swoft

  1、Task

  2, coroutine

 

A: Task task:

  1, arranged, adding app / bean.php file

  'httpServer' => [
        // ...
        'on'       => [
            SwooleEvent::TASK   => \bean(TaskListener::class),  // Enable task must task and finish event
            SwooleEvent::FINISH => \bean(FinishListener::class)
        ],
        /* @see HttpServer::$setting */
        'setting'  => [
            'task_worker_num'       => 12,
            'task_enable_coroutine' => true,
            'worker_num'            => 2
        ]
    ],
View Code

  2, regular tasks

1, the require installation swoft Composer / the crontab 
2, disposed 
 'HTTPServer' => [ 
            // ... 
            'Process' => [ 
                'the crontab' => the bean (Swoft \ the Crontab \ Process \ CrontabProcess :: class) 
            ], 
            // ... 
        ], 

3, defined in / app / Task / Crontab / file New folder 

<PHP? 

namespace App \ Task \ Crontab; 

use Swoft \ Crontab \ annotaion \ Mapping \ Cron; 
use Swoft \ Crontab \ annotaion \ the Mapping \ the Scheduled; 

/ ** 
 * class DemoCronTask 
 * @package the App \ the task \ the Crontab 
 * 
 * @Scheduled (name = "demoCronTask") // declaration timer task 
 * / 
class DemoCronTask { 

    / **  
     * @Cron ( "*") // execute per second
     * /
    public function secondTask(){
        var_dump("--111----",date('Y-m-d H:i:s', time()));
    }

    /**
     * @Cron("0 * * * * *") //每分钟执行
     */
    public function miunteTask(){
        var_dump("222------",date('Y-m-d H:i:s', time()));
    }
}
View Code

     3, coroutine, asynchronous tasks

  (1), a mission statement, in / app / Task / Task / Folder New

<?php declare(strict_types=1);

namespace App\Task\Task;

use Swoft\Task\Annotation\Mapping\Task;
use Swoft\Task\Annotation\Mapping\TaskMapping;

/**
 * Class DemoTask
 *
 * @since 2.0
 * @Task(name="demoV2Task")  //标记类是一个任务
 */
class DemoTask
{
    /**
     * @TaskMapping(name="list")  //映射名称
     *
     * @param int    $id
     * @param string $default
     *
     * @return array
     */
    public function getList(int $id): array
    {
        var_dump("------------");

        sleep(5);

        return [
            'list'    => [1, 3, 3],
            'id'      => $id
        ];
    }

    /**
     * @param int $id
     * @return bool
     *
     * @TaskMapping(name="putLists")
     */
    public function putList(int $id) : bool
    {
        if($id > 5)
            return true;

        return false;
    }
}
View Code

       (2), task delivery

// coroutine delivery 
$ Data :: = the Task CO.'S ( 'demoV2Task', 'List', [12 is]); 

// asynchronous delivery 
$ data = Task :: async ( ' demoV2Task', 'list', [12]) ;

      (3), asynchronous delivery if the need to focus on asynchronous task processing result, listeners can be added in the folder / app / Task / Listener / under New File

<?php

namespace App\Task\Listener;

use Swoft\Log\Helper\CLog;
use function context;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Task\TaskEvent;

/**
 * Class DemoListener
 * @package App\Task\Listener
 * @Listener(event=TaskEvent::FINISH)  //参数必须带Finsh
 */
class DemoListener implements EventHandlerInterface{

    /**
     * @param EventInterface $event
     *
     * @throws \Swoft\Exception\SwoftException
     */
    public function handle(EventInterface $event): void
    {
        // TODO: Implement handle() method.
        $fId = context()->getTaskUniqid();

//        var_dump($fId);
        CLog::info(\context()->getTaskUniqid());

        $taskData = context()->getTaskData();
//        var_dump($taskData);

        CLog::info(\context()->getTaskData());
    }
}
View Code

 

Note: Task is not yet clear there are two places

      1, when the delivery is blocked coroutine.

  2, asynchronous monitor where all listeners only with the parameters @Listener (event = TaskEvent :: FINISH) will be executed again.

 

View the document: 

    https://www.swoft.org/docs/2.x/zh-CN/task/index.html    

    

 

 

Guess you like

Origin www.cnblogs.com/cj8988/p/11607593.html