如何在后台休眠PHP(Laravel 5.2) [英] How to sleep PHP(Laravel 5.2) in background
问题描述
我创建了一个artisan command
,要在调用方法后立即运行.但是该命令包含sleep();
命令.我想在后台运行该 artisan命令,因为该方法需要return
立即响应用户.我的示例代码如下:
I have created an artisan command
that I want to run immediately after a method called. But the command contains a sleep();
command. I want to run that artisan command in background because the method need to return
immediately response to user. My sample code is a below:
在路由文件中
Route::get('test', function(){
Artisan::queue('close:bidding', ['applicationId' => 1]);
return 'called close:bidding';
});
在close:bidding
命令中
In close:bidding
command
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时,加载浏览器10秒钟后将显示返回字符串called close:bidding
,因为命令内有一个sleep(10 * 1000)
.
Problem
When I hit /test
url the return string called close:bidding
is being shown after 10 seconds browser loading, because there is a sleep(10 * 1000)
inside the command.
我想要的
我想在后台运行命令.我的意思是,当我点击/test
网址时,它应该立即显示called close:bidding
,但是close:bidding
命令将在后台运行. 10秒钟后,它会更新应用程序,尽管前端用户不会注意到任何有关它的信息.
What I want
I want to run the command in background. I mean when I hit the /test
url it should immediately show up the called close:bidding
but close:bidding
command will be running in background. After 10 seconds it will update the application though the front-end user won't notice anything anything about it.
部分问题
-
它与
multi threading
有关系吗?
这是使用PHP无法解决的问题,我应该换个角度吗?
Is it something that cannot be solve using PHP, should I think differently?
即使使用Laravel队列也可以解决此问题吗?
Is there any way to solve this problem even using Laravel queue?
推荐答案
创建工作
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class JobTest implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
private $payload = [];
public function __construct($payload)
{
$this->payload = $payload;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$appId = $this->payload('applicationId');
//following code line is making the problem
sleep(1000 * 10);
}
}
推送作业后台队列
Route::get('test', function(){
dispatch((new JobTest)->onQueue('queue_name'));
return 'called close:bidding';
});
在这种状态下,我们有工作,您将工作转移到队列中.但尚未处理.我们需要队列侦听器或工作者在后台处理这些工作
At this state we have job and you dispath the job to the queue. But it not processed yet. We need queue listener or worker to process those job in background
php artisan queue:listen --queue=queue_name --timeout=0
OR
php artisan queue:work --queue=queue_name --timeout=0 //this will run forever
注意: 也许您可以尝试使用超级用户,beanstakd来管理队列
Note: May be you can try supervisord,beanstakd for manage queue
有关更多信息,请参见此
For more info refer this
这篇关于如何在后台休眠PHP(Laravel 5.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!