【发布时间】:2017-05-03 12:30:20
【问题描述】:
我创建了一个artisan command,我想在调用方法后立即运行它。但是该命令包含一个sleep(); 命令。我想在后台运行那个 artisan 命令,因为该方法需要return 立即响应用户。我的示例代码如下:
在路由文件中
Route::get('test', function(){
Artisan::queue('close:bidding', ['applicationId' => 1]);
return 'called close:bidding';
});
在close:bidding 命令中
public function handle()
{
$appId = $this->argument('applicationId');
//following code line is making the problem
sleep(1000 * 10);
//close bidding after certain close time
try{
Application::where('id', $appId)->update(['live_bidding_status' => 'closed']);
}catch (\PDOException $e){
$this->info($e->getMessage());//test purpose
}
$this->info($appId.": bid closed after 10 seconds of creation");
}
问题
当我点击/test url 时,返回字符串called close:bidding 在浏览器加载10 秒后显示,因为命令中有sleep(10 * 1000)。
我想要什么
我想在后台运行命令。我的意思是当我点击/test url 时,它应该立即显示called close:bidding 但close:bidding 命令将在后台运行。 10 秒后,它会更新应用程序,但前端用户不会注意到任何有关它的信息。
部分问题
它是否与
multi threading有某种关系?是不是用 PHP 解决不了的问题,我应该换个思路吗?
使用 Laravel 队列有没有办法解决这个问题?
【问题讨论】:
-
也许你可以试试 laravel 队列作业而不是命令
-
谢谢,我试试
标签: php multithreading laravel-5 sleep