Laravel 5:执行异步任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32188076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:14:44  来源:igfitidea点击:

Laravel 5: Performing an asynchronous task

phplaravelasynchronous

提问by John Bupit

I have the following function where I process an image file of type UploadedFile, give it as a random name, use \Intervention\Imageto store it in different sizes, and then return the image name.

我有以下函数,我处理类型为 的图像文件UploadedFile,将其作为随机名称,用于\Intervention\Image以不同的大小存储它,然后返回图像名称。

The storing part takes lot of time and the request usually times out. It would be great if I could do the processing in a separate thread or process, and return the image name. Here's the code I have:

存储部分需要很多时间,并且请求通常会超时。如果我可以在单独的线程或进程中进行处理并返回图像名称,那就太好了。这是我的代码:

public function storeSnapshots(UploadedFile $image) {
    // Generate a random image name.
    $imageName = str_random(12) . '.' . $image->getClientOriginalExtension();

    // Process the image. Should be done asynchronously.
    \Intervention\Image\Facades\Image::make($image)
        ->heighten(2000, function($constraint) {
            $constraint->upsize();
        })->save('img/lg/' . $imageName)
        ->heighten(800)->save('img/md/' . $imageName)
        ->heighten(120)->save('img/sm/' . $imageName);

    // Return the image name generated.
    return $imageName;
}

What would be the Laravel way to perform an asynchronous task?

Laravel 执行异步任务的方式是什么?

回答by ceejayoz

Laravel performs asynchronous tasks via the queue system.

Laravel 通过队列系统执行异步任务。