Laravel 5:执行异步任务

Joh*_*pit 1 php asynchronous laravel

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

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

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

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

cee*_*yoz 8

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