DEV Community

Cover image for PHP is a Single-Threaded Language, So How Does Laravel Handle Queue Jobs Asynchronously?

PHP is a Single-Threaded Language, So How Does Laravel Handle Queue Jobs Asynchronously?

Mahfuzur Rahman on November 28, 2024

PHP is known as a single-threaded language, meaning it can only execute one task at a time within a single process. However, Laravel provides a rob...
Collapse
 
ilearnbydoing profile image
Durgesh Gupta

There is something called Laravel Octane you must checkout it works well with high-powered application servers, including FrankenPHP, Open Swoole, Swoole, and RoadRunner. Octane boots your application once, keeps it in memory, and then feeds it requests at supersonic speeds.

Collapse
 
devmahfuz profile image
Mahfuzur Rahman

Thanks for your suggestion. I will definitely look into it.

Collapse
 
shawn_mcallister_109fd3fb profile image
Shawn McAllister

PHP itself is single threaded but most httpd servers are multi-threaded.

Collapse
 
vicentimartins profile image
Vicente Martins

Perfect! To deep on any tech is the best way to learn and develop itself. Congrats!!!

Collapse
 
perisicnikola37 profile image
Nikola Perišić

Thank you for sharing this

Collapse
 
miguelgilmartinez profile image
Miguel Gil Martínez

What about fibers? Is it not used by Laravel to achieve multiparalelism?

Collapse
 
devmahfuz profile image
Mahfuzur Rahman

Actually no, php fiber is not a multi-paralelism. It just helps you to stop a function's execution and later resume it. It doesn't run in background or in a separate thread. And when you resume a fiber function it runs synchronously in the main PHP process.

Collapse
 
markuszeller profile image
Markus Zeller

I would prefer calling it concurrency instead of parallelism.

Collapse
 
dan_le_brown profile image
Brown • Edited

Based on my understanding of the two concepts, I believe parallelism is the most-suited word:
Each spun worker executes "just" one task per time and they do so in parallel.

This is different from a concurrent operation where you have "just" one worker executing various tasks but toggling between them as each task runs into an operation, say, a network call, that cannot be resolved in that same moment (but will be, later).
The same worker keeps toggling/switching until all the tasks are executed eventually

In parallelism, you have processes like this:

worker 1
o – o – o – o

worker 2
o – o – o – o – o – o

In concurrency, you have a process like this:

worker 1
Image description

(where "o" signifies the executed task, and "–' represents the current path of execution)

Let me know if this makes any sense.

Collapse
 
devmahfuz profile image
Mahfuzur Rahman

Nicely explained !!

Thread Thread
 
dan_le_brown profile image
Brown

Thank you for your kind words! It's something that has confused me in the past, so it feels great to be able to articulate the difference to another person

Collapse
 
markuszeller profile image
Markus Zeller

I get your point and totally agree for modern systems and simplified explanation.

Here's my view: Technically speaking, I think it depends on the system. As long as each worker runs on its own CPU at the same time, it is real parallelism. If you have only one CPU, there is no parallelism, but you can achieve it looking the same with concurrency. Note, that even multi-threading may still be concurrency, the OS handles it for us.

Thread Thread
 
dan_le_brown profile image
Brown

Ah, thank you for elaborating. I like your point about how the deciding factor is the no of CPUs. It's intuitive and easier to understand

Collapse
 
eshimischi profile image
eshimischi

ReactPHP reactphp.org/

Collapse
 
mohmd_naeem_k profile image
Mohammed Naeem

Actually you can do this in native php with pheanstalk (php library for beanstalkd queue management) ... Just run the php watcher script using supervisor.

Collapse
 
jaskaran_singh_4d13d7c4e1 profile image
Jaskaran Singh

Good

Collapse
 
dilsha_test_b7f4fbd18c650 profile image
dilsha Test

Perfect 🔥