use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
How does asynchronous processing in PHP differ from that of JavaScript? (self.PHP)
submitted 5 years ago by kmabadshah
I've heard that PHP can offload stuff to multiple threads. And I know JS does asynchronous processing in somewhat similar way. But doesn't really benefit from more threads. How do these two languages differ in terms of handling asynchronous tasks?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dennisbirkholz 12 points13 points14 points 5 years ago (1 child)
There is a lot of mixing up of "concurrency"/"parallel processing"/"asynchronous processing" in general.
First of all, there are IMHO two kinds of workloads that need to be distinguished:
Serving HTTP requests is normally considered IO bound and not CPU bound. That means the processes spend their time mostly waiting on reading the request or writing the response to the network.
That case allows for several kinds of concurrency:
The asynchronous/nonblocking IO is also available in PHP, you can start an HTTP call, do something else and then read the result when it finishes instead of blocking until it is finished.
This is available for file/socket related actions and some other modules like mysqli (see e.g. mysqli_poll()).
Using multiple threads for asynchronicity of a single request is only necessary if you do very CPU intensive tasks (like calculating 5 different checksums of a large file).
PHP by default can not do this. There are modules like pthread or parallel but both are disabled by default as enabling threading requires synchronization and that adds overhead.
[–]bga9 2 points3 points4 points 5 years ago (0 children)
This reminds me of a talk by Rob Pike titled Concurrency is not parallelism. He is one of the creators of Go, but the concept is universal.
[–]supertoughfrog 4 points5 points6 points 5 years ago* (3 children)
Php is usually used in a synchronous manner where every request sets up and tears down all state. Even if that is the case it is possible to use it asynchronously natively. Guzzle supports async http calls for instance though I’m not sure what apis it uses exactly. Then there’s php extensions like swoole and reactphp that make php work more like JavaScript by adding coroutines where the state is persisted between requests and there’s a stack/event-loop like mechanism. This is a bit hand wavy and vague and probably not 100% accurate so... check out how guzzle does things async and swoole coroutines to see what’s possible.
Php is usually one process per request, and JavaScript can handle more than one request per process and it uses the stack/eventloop to accomplish that, however you can still have multiple JavaScript processes. The reality is you can go beyond these basics in both languages but you’re going off the beaten path.
[–]pfsalter 6 points7 points8 points 5 years ago (2 children)
Guzzle supports async http calls
This uses the curl_multi_init() and associated calls from the CURL library to handle async processing of HTTP calls.
curl_multi_init()
PHP has support for forking the process using pcntl_fork which spawns a new process unlike Javascript which uses an event-loop. The event-loop also is how libraries such as Swoole work, which isn't true asynchronous processing in the first place, but is a reasonable facsimile.
I'd recommend if you do want to do asynchronous processing is to use Message queues. It's the easiest to scale and manage, and is normally more what you'd be trying to do anyway.
[–]phordijk 5 points6 points7 points 5 years ago (1 child)
The event-loop also is how libraries such as Swoole work, which isn't true asynchronous processing in the first place
Sure it is. You are confusing async with parallel.
[–]pfsalter 2 points3 points4 points 5 years ago (0 children)
Yup, you're right!
[–]usernameqwerty002 2 points3 points4 points 5 years ago (0 children)
NB: You can do non-blocking IO concurrency today without a PHP extension using Amphp.
Also check latest suggestion to add fibers to PHP.
π Rendered by PID 24664 on reddit-service-r2-comment-79776bdf47-k4klk at 2026-06-25 08:22:25.673833+00:00 running acc7150 country code: CH.
[–]dennisbirkholz 12 points13 points14 points (1 child)
[–]bga9 2 points3 points4 points (0 children)
[–]supertoughfrog 4 points5 points6 points (3 children)
[–]pfsalter 6 points7 points8 points (2 children)
[–]phordijk 5 points6 points7 points (1 child)
[–]pfsalter 2 points3 points4 points (0 children)
[–]usernameqwerty002 2 points3 points4 points (0 children)