PHP code example of sanderswang / async

1. Go to this page and download the library: Download sanderswang/async library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sanderswang / async example snippets


php 异步任务库
一个快速响应的web应用必然有很多繁重的异步任务去做,Async利用pnctl_fork创建异步进程,同时管理异步任务。

use Async/Async;
$async = new Async($redis_host,$redis_port,$redis_password,'/tmp/async);
$async->task(function($data){
    echo 'common Async' . "$data\n";
    doSomething();
}, 'common task');

use Async/Async;
$a = new Async($redis_host,$redis_port,$redis_password,'/tmp/async');
//当任务不存在时创建任务
if(!$a->isTaskExists($task_name)){
    $a->task(function($data){
        echo 'hello Async'."$data\n";
        doSomething();
    },$task_name,true,true,'123');
}

$a = new Async($redis_host,$redis_port,$redis_password,'/tmp/async');
for($i = 0;$i < 10;$i ++){
    sleep(2);
    $a->sendData($task_name, "这是我第$i个数据");
}