Nodejs Learning notes _nodejs and PHP differences in infrastructure shared state concurrency

Source: Internet
Author: User
The vast majority of the discussions on node. JS focus on dealing with high concurrency capabilities, making sure to understand the tradeoffs within node and the reasons for the good performance of node applications.

Node introduces a complex concept to javascript: The concurrency of shared state.


Node takes a long-running process

and PHP will produce multiple processes in Apache.

As shown in the following:

Code Validation:

Php:

!--? php$i = 0; $i ++;echo $i  

Nodejs:

var http = require (' http '); var i=0;http.createserver (  function () {i++ console.log (i)}). Listen ("127.0.0.1"); 

when using a browser to request these two addresses

php will always output 1

node. JS Outputs 1 2 3 4 5 6 ...

through the picture, you can see that after PHP executes the program, the next execution will reapply for a new thread. Each execution of a variable requests memory, is assigned a value of 0, plus 1, so the output is 1

and Nodejs will maintain a long-running process, the variable i in memory always exists, each execution will be added one, and therefore will appear 1 2 3 4 5 6. .


Apache generates new threads each time the state is refreshed, $i will be re-assigned,

node does not, every time will give $i plus 1

Therefore, node, you need to modify the callback function in memory variables in particular care.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.