PHP code example of flc / php-queue
1. Go to this page and download the library: Download flc/php-queue 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/ */
flc / php-queue example snippets
/**
* 任务创建
*/
use Jobs\Demo;
// 创建工作
$demo = new Demo('测试');
// 推送到队列
for ($i = 0; $i <= 100; $i ++) {
Manager::instance()->push($demo);
}
echo Manager::instance()->count();
/**
* 任务执行者(常驻)
*/
if ('cli' !== php_sapi_name()) {
die('必须在命令行模式下运行');
}
while (true) {
// 从队列拉取任务
$job = Manager::instance()->pull();
// 如无任务,则休息2秒
if (! $job) {
sleep(2);
continue;
}
try {
call_user_func_array([$job, 'handle'], []);
} catch (Exception $e) {
echo $e->getMessage();
}
}
composer