PHP code example of baiy / think-async
1. Go to this page and download the library: Download baiy/think-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/ */
baiy / think-async example snippets
use Baiy\ThinkAsync\Facade\Async;
// 异步执行代码
Async::exec($className,$methodName,...$params);
// 异步执行代码使用自定义队列
Async::execUseCustomQueue($className,$methodName,$queue,...$params);
// 异步延迟执行代码
Async::delay($delay,$className,$methodName,...$params);
// 异步延迟执行代码使用自定义队列
Async::delayUseCustomQueue($delay,$className,$methodName,$queue,...$params);
namespace app\controller;
use Baiy\ThinkAsync\Facade\Async;
use app\BaseController;
use think\facade\Log;
class Index extends BaseController
{
public function index()
{
// 异步执行
Async::exec(self::class, 'test', 'exec');
Async::execUseCustomQueue(self::class, 'test','async_exec_method_custom', 'exec');
// 异步延迟执行 延迟20秒
Async::delay(20, self::class, 'test', 'delay');
Async::delayUseCustomQueue(20, self::class, 'test','async_exec_method_custom', 'delay');
return '';
}
public static function test($type)
{
Log::info("异步执行的方法 {$type}");
}
}
use Baiy\ThinkAsync\Facade\Async;
// 事件触发
Async::trigger($name,...$params);
use Baiy\ThinkAsync\Facade\Async;
use Psr\Log\LoggerInterface;
/** @var LoggerInterface $log */
Async::setLog($log);
use Baiy\ThinkAsync\Facade\Async;
// 获取所有队列标示
Async::queue();
// 获取队列长度
Async::queueSize($queue);
// 获取队列名称
Async::queueName($queue);
config/async.php
## 执行下方命令会输出相关的队列监听命令
php think async:show
## echo
======= async_exec_method =======
listen mode:php think queue:listen --queue async_exec_method
work mode:php think queue:work --queue async_exec_method
======= async_delay_method =======
listen mode:php think queue:listen --queue async_delay_method
work mode:php think queue:work --queue async_delay_method
======= async_subscribe_demo =======
listen mode:php think queue:listen --queue async_subscribe_demo
work mode:php think queue:work --queue async_subscribe_demo