W
W
websiller2018-08-26 21:08:03
PHP
websiller, 2018-08-26 21:08:03

Node js is single threaded and php is multithreaded?

The nodejs server, when a new client connects, calls a callback in which the request is processed. If there are heavy calculations in the callback that take a long time, then all other clients connected after that will be queued in the event loop, and only after the completion of the current callback will the next one be executed.
In php, even if there are heavy calculations in the script, then two different clients will be served in parallel, without waiting for each other to complete.
Is it possible to achieve this in nodejs?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
V
Vitaly Stolyarov, 2018-08-26
@websiller

Yes. WebWorkers, clusters

P
PloAl, 2018-08-26
@PloAl

Your first statement is not entirely correct.
If the "heavy" callback is fully synchronous code then true. If asynchronous code is used, other requests will not wait.
In php, a new process is started to handle each request.
You can also use PM2 for multiple processes.

I
Ivan Shumov, 2018-08-27
@inoise

Don't confuse concurrency with the asynchronous model. PHP is single-threaded out of the box and concurrency is achieved through webserver workers. Nodejs uses clusters or multiple workers on different ports as the backend for the webserver. The Nodejs limit is 1 process per core for obvious reasons.
The asynchronous model is both there and there, but in nodejs it’s easier with it because the process dies after processing (if it’s not a written daemon
) next client.
About manual deployment (wrote above) is nonsense. There's been a ton of ci/cd systems out there for a long time, not to mention serverless power in AWS and Azure

O
OnYourLips, 2018-08-26
@OnYourLips

PHP uses processes by default. Noda also knows how to do them.
PHP, just like a node, can work with asynchrony.
And they both have Threads, but both do it poorly so far.

A
Andrey K, 2018-08-27
@kuftachev

PHP is single-threaded, but Nginx or Apache has a certain number of workers to process requests.
You can also run many instances in Node.js. The question is understanding what you are doing.
Also, don't forget that you can use queues to process heavy calculations, for example, you get a file to the server and queue it for processing, and then resize it in a separate process. But this is just an example, it all depends on the task.

N
nic777, 2018-09-27
@nic777

php itself is single-threaded, but a webserver can run multiple php instances.
If you run a heavy report that thinks for five minutes, a hundred other users can run three of their scripts in those five minutes. They won't wait for yours to complete.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question