我正在尝试实现队列,但结果不是异步的 我已经应用了以下内容
config/queue.php
'default' => env('QUEUE_DRIVER', 'database'),
'connections' => [
'sync' => [
'driver' => 'sync',
],
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
],
]
然后应用以下命令 php artisan 队列:表 php artisan 迁移
然后运行
php artisan queue:listen
这是功能
SomethingController.php
$model1 = new \App\Model1;
public function store(){
Log::debug('before dispatch');
$this->dispatch(new SendGiftCommand($model1));
Log::debug('before dispatch');
return true;
}
SendGiftCommand.php
{
Log::debug('handle');
SendGift::SendGiftAgedBased($this->model1);
sleep(4);
Log::debug('SendGiftCommand end');
}
SendGift.php
public static function SendGiftAgedBased(Model1 $model1){
Log::debug('inside SendGiftAgedBased');
}
即使进程已经运行但它不是异步的,它等待命令完成以在 Controller 中返回响应
然后我按这个顺序 git 日志
[2015-12-09 16:28:42] local.DEBUG: before dispatch
[2015-12-09 16:28:42] local.DEBUG: handle
[2015-12-09 16:28:42] local.DEBUG: inside SendGiftAgedBased
[2015-12-09 16:28:46] local.DEBUG: SendGiftCommand end
[2015-12-09 16:28:46] local.DEBUG: after dispatch
它应该在 Localhost 上工作吗?
最佳答案
我遇到了同样的问题,作业不是异步的,这对我有用:
- 编辑 .env 并将 QUEUE_DRIVER 设置从 sync 更改为 database(编辑 config/queue.php 还不够)
- 重新启动您的流程
关于php - Laravel 中的异步队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34181644/