Skip to content

normal-ai/thinkphp6-async-task

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

thinkphp6-async-task

介绍

基于workerman的thinkphp6异步任务模型

安装

composer require cathy/thinkphp6-async-task

使用

创建异步任务类及方法

<?php
namespace app\tasks;

use cathy\AsyncTask\main\AsyncTaskSynchronizer;

class DemoAsyncTasks
{
    public function demo($data, AsyncTaskSynchronizer $taskSynchronizer) {
        for($i = 1;$i <= 100; $i++) {
            $taskSynchronizer->update($i);
            sleep(1);
        }
        $taskSynchronizer->done(['message'=>'Hi!'.$data['name']]);
    }
}

执行异步任务

$asyncTaskProducer = new AsyncTaskProducer();
try {
    $taskKey = $asyncTaskProducer->execute(
        new AsyncTask(DemoAsyncTasks::class, 'demo', ['name'=>'cathy'])
    );
    echo $taskKey;
} catch (ExecAsyncTaskException|InvalidAsyncTaskException $e) {
    echo $e->getMessage();
}

获取任务进度

try {
    $taskKey = ''; // 执行异步任务得到的taskKey
    echo json_encode(AsyncTaskAcquirer::get($taskKey));
} catch (Exception $e) {
    echo $e->getMessage();
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy