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

Source: Internet
Author: User

Most of the discussions on node. JS focus on dealing with high concurrency capabilities, making sure to identify the tradeoffs within node and the reasons for node application performance.

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


Node uses a long-running process

and PHP will produce multiple processes in Apache.

For example, as seen in :

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 (3000, ' 127.0.0.1 ');

When using a browser to request these two addresses, PHP will always output 1node.js will output 1 2 3 4 5 6 ... Through the picture, it can be seen that after the application of PHP running, the next run will again apply for a new thread. Each run of the variable will request memory, assign a value of 0, plus 1, so the output is 1 and Nodejs will maintain a long-running process, the variable i in memory exists, each execution will be added one, so there will be 1 2 3 4 5 6 ...
The new thread generated by Apache will refresh every time, $i will be assigned again, and node will not, and will give $i 1 each time. Therefore, you need to be especially careful when you change the variables in memory for the callback function in node.

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.