-1

What are best way to call laravel controller actions in async manner as its very easy with .net using async await keywords.

here is .net example

public async Task<ViewResult> Index() { 
     return View(await GetThingsAsync());
}

does it doable in laravel same way ?

does PHP laravel provide any helper or plugin that can help to call controller actions in async manner and all DB queries in async manner / does eloquent provide support for asynchronous queries

Phil
  • 157,677
  • 23
  • 242
  • 245
user2797910
  • 61
  • 2
  • 11
  • Does this answer your question? [How to make function run in background in laravel](https://stackoverflow.com/q/31451787/283366) – Phil Jan 12 '22 at 02:55
  • @Phil not in background . same way as async await works in .net . like I have updated question with .net example – user2797910 Jan 12 '22 at 03:09
  • Do you know what you're actually asking? PHP does not have .NET's asynchronous programming model or reactive streams (similar model) in the JVM, it is mostly single-threaded. While those asynchronous / reactive models are good for performance, if you're simply waiting on results (via `await`) then there's nothing to gain in PHP by trying to do similar; it simply does not have those features. – Phil Jan 12 '22 at 03:14
  • i know it single thread by nature . but there are many packages that implement multithreading in PHP like her e is one https://dev.to/webong/using-asynchronous-processes-in-php-7io. i just want to call all DB queries in async manner that I have written in laravel controller – user2797910 Jan 12 '22 at 03:33
  • _"i just want to call all DB queries in async manner"_... why? If you're after parallelisation (which is very different to .NET's async / await), why not just use that `spatie/async` library from the article you linked? – Phil Jan 12 '22 at 03:34
  • that is a API controller and many clients are accessing it I want to reduce waiting time for them . – user2797910 Jan 12 '22 at 03:37
  • That is not a problem that is solved via asynchronous programming. – Phil Jan 12 '22 at 03:43
  • okay then what you can suggest about it – user2797910 Jan 12 '22 at 03:44
  • That's an extremely broad topic but horizontally scaling infrastructure and load balancing would be good starting places. It also sounds like you're prematurely optimising... until you know you have performance issues, you can't possibly plan on how to handle them – Phil Jan 12 '22 at 03:47

1 Answers1

0

@Phil i have added a spatie/async package to my laravel project solution and changed my controller code like this .

public function indexAsync(Request $request)
{
    $results=null;

    $pool = Pool::create();

    $pool[] = async(function () {
    return Test::all();
     })->then(function ($output) {
    $this->results=$output;
    });

    await($pool);

    return view('test.index',['results' => $this->results]);
}

I am getting the expected result on view .

does it work as I want or it can't help to achieve multithreading in php

Plastic
  • 9,874
  • 6
  • 32
  • 53
user2797910
  • 61
  • 2
  • 11