php 在 windows 下多進(jìn)程不好使,沒有 fork斤葱,不過你還是可以通過啟動(dòng)多個(gè) php.exe 來執(zhí)行腳本慷垮,把公共隊(duì)列數(shù)據(jù)放在數(shù)據(jù)庫(kù)或者 redis 下,然后排隊(duì)訪問揍堕。
不過這個(gè)東西已經(jīng)由 Laravel 實(shí)現(xiàn)了料身,Laravel 提供了一個(gè)消息隊(duì)列的投遞和監(jiān)聽框架,消息隊(duì)列的后端可以是
- sync 就是同步方法鹤啡,一邊生產(chǎn)數(shù)據(jù)一邊消費(fèi)數(shù)據(jù)惯驼,用來測(cè)試隊(duì)列方法蹲嚣,生產(chǎn)環(huán)境不使用
- database 采用數(shù)據(jù)庫(kù)來存儲(chǔ)消息隊(duì)列數(shù)據(jù)
- beanstalkd
- sqs
- redis
后面三種都是消息隊(duì)列服務(wù)第三方組件递瑰,Laravel 在隊(duì)列任務(wù)失敗時(shí),會(huì)把它存到數(shù)據(jù)庫(kù)的 failed_jobs
表隙畜。
消息隊(duì)列消費(fèi)者
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class EchoTest implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($messageid)
{
$this->messageid_ = $messageid;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
echo "hello queue world!" . $this->messageid_;
}
protected $messageid_;
}
消息隊(duì)列數(shù)據(jù)生產(chǎn)者
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Jobs\EchoTest;
class TestDb extends Command
{
use DispatchesJobs;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'testdb {--queue}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$options = $this->options();
$queueName = $this->option('queue');
$this->info('hello world!');
$this->info('中文測(cè)試');
$this->error('something went wrong!');
$this->info('通過命令行執(zhí)行隊(duì)列任務(wù)');
for ($i = 0; $i < 1000; ++$i) {
$this->dispatch((new EchoTest($i))->onQueue('echo'));
}
}
}
修改 .env 配置
QUEUE_DRIVER=database
運(yùn)行一個(gè)消息隊(duì)列監(jiān)聽進(jìn)程
cd /d H:\laravel_cmd_test
php artisan queue:work database --daemon --queue=echo
運(yùn)行消息隊(duì)列生產(chǎn)者命令行
php artisan testdb
為什么要在 windows 下使用 laravel 實(shí)現(xiàn)消息隊(duì)列
- 不用 C++ 實(shí)現(xiàn)抖部,用 asio 是可以但是不方便
- 用 node 也可以,后端搭配 redis 使用也可以议惰,失敗任務(wù)隊(duì)列還得持久化到 mysql 里
- PHP 方便操作 mysql慎颗,可調(diào)用 C++ 命令行程序;數(shù)據(jù)方便入庫(kù)言询,腳本修改邏輯方便
- Laravel 框架很強(qiáng)大俯萎,生成數(shù)據(jù)表和測(cè)試數(shù)據(jù)很方便,失敗隊(duì)列任務(wù)重試輕松定制
- 在 Laravel 的后臺(tái)工作進(jìn)程使用數(shù)據(jù)庫(kù)長(zhǎng)連接
添加批處理命令行啟動(dòng)多個(gè)消費(fèi)者進(jìn)程
** run-sub-daemon.bat **
title %1
cd /d "%~dp0"
@path=%~dp0\tools;%path%
CALL "%~dp0\setenv.bat"
cd /d "%~dp0"
cd db_test\laravel_cmd_test
php artisan queue:work database --daemon --queue=echo
run-daemon.bat
@echo off
setlocal enabledelayedexpansion
:main
for /l %%i in (1,1,5) do (
set n=%%i
start "" "run-sub-daemon.bat" 隊(duì)列子進(jìn)程!n!
)
雙擊運(yùn)行 run-daemon.bat运杭,打開 5 個(gè)隊(duì)列消費(fèi)者進(jìn)程夫啊。
然后運(yùn)行 laravel 命令行 php artisan testdb
,開始狂奔吧!
![$O{LJ_UR7}WWOKK2O}BDC]A.png](http://upload-images.jianshu.io/upload_images/1111419-e62d6ec4e61a2471.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)