小编Imr*_*ran的帖子

如何让 PHP(Laravel 5.2) 在后台休眠

我创建了一个artisan command我想在调用方法后立即运行的程序。但命令中包含sleep();命令。我想在后台运行该 artisan 命令,因为该方法需要return立即响应用户。我的示例代码如下:

在路线文件中

Route::get('test', function(){
    Artisan::queue('close:bidding', ['applicationId' => 1]);
    return 'called close:bidding';
});
Run Code Online (Sandbox Code Playgroud)

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");
    } 
Run Code Online (Sandbox Code Playgroud)

问题 当我点击url 时,浏览器加载 10 秒后就会显示/test返回字符串,因为命令中有一个。called close:biddingsleep(10 * 1000) …

php multithreading sleep laravel-5

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

laravel-5 ×1

multithreading ×1

php ×1

sleep ×1