Mastering Nova is the first comprehensive, end-to-end, course that is launched by Bruno Falcao that covers all the aspects of Laravel Nova. The course was structured for anyone that wants to learn Nova, from beginners to advanced level.

A lightweight solution for running PHP code concurrently

Original – by Freek Van der Herten – 1 minute read

Our team released a new package called spatie/fork. Using this package you can easily execute multiple pieces of code concurrently.

In this example, where we are going to call an imaginary slow API, all three closures will run at the same time.

use Spatie\Fork\Fork;

$results = Fork::new()
    ->run(
        fn () => (new Api)->fetchData(userId: 1),
        fn () => (new Api)->fetchData(userId: 2),
        fn () => (new Api)->fetchData(userId: 3),
    );

$results[0]; // fetched data of user 1
$results[1]; // fetched data of user 2
$results[2]; // fetched data of user 3

If you want to know more about why we created the package, check out this blogpost by my colleague Brent.

In the video below, which was streamed live, you can see me explain the internals.

To know more about how to use the package, head over to the readme of spatie/fork on GitHub.

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

Webmentions

Jens liked on 6th May 2021
William Rodriguez liked on 4th May 2021
ali ali liked on 4th May 2021
Salman Zafar liked on 4th May 2021
José Cage liked on 4th May 2021
Nuno Souto liked on 3rd May 2021
QMAS.vn retweeted on 3rd May 2021
René Sinnbeck liked on 3rd May 2021
Tauseef shah liked on 3rd May 2021
Spatie retweeted on 3rd May 2021
Adrian Crisan liked on 3rd May 2021
Deven Jahnke retweeted on 3rd May 2021
David H. Sianturi liked on 3rd May 2021
Xin 💻🚀 liked on 3rd May 2021
João Alves liked on 3rd May 2021
Amitav Roy replied on 3rd May 2021
Wow, that's awesome. I will also try to implement this and check. I have a few situations where I execute multiple queries and they are not dependent on each other. So, it will be nice.
João Alves replied on 3rd May 2021
Tried this with mysql querys for a big report and my querys went for 4s to 1s. Now I am trying to improve it a bit more. I will let you know how this go.
Zubair Mohsin liked on 3rd May 2021
Calefyb retweeted on 3rd May 2021