About the process: different Nodejs and Golang

Source: Internet
Author: User
Tags switches

Nodejs and Golang are supporting the process, from the performance point of view, Nodejs for the support of the process is async/await,golang to the support of the association is goroutine. On the topic of the co-process, simply speaking, it can be seen as a non-preemptive lightweight thread.

The co-process itself

In a nutshell, the above mentioned

"Can be thought of as a non-preemptive lightweight thread."

In multi-threading, where a piece of code is executed in a thread, the CPU automatically divides the code into fragments and switches the CPU control at a certain time, and the thread makes sure that the resources it uses are modified when the CPU executes other threads ' Code (memory stacks, hard disk data resources, etc.) through a lock mechanism ,

Thread A creates a variable in a block of memory, the thread a code is not finished, the CPU switches to execute thread B, but because of the lock, thread B cannot use the memory.

Multi-threaded and multi-coprocessor are not any different when viewed only on single-threaded CPUs, because threads cannot be parallel and can only be performed by CPU fragmentation.

The process is similar to this meaning. The co-process is something inside the thread (for the time being, it's not about multithreading), and when the coprocessor encounters a block, it switches thread control and lets the thread perform another process, except that it's queued. This is one thing to do with Nodejs's event polling, and in Nodejs, let the system know that I'm going to

Read the file, the system read, Io blocked, Nodejs to execute the next section of code B, after the execution of the check whether the blocking wait, if the wait is complete to push the results behind the event queue to execute the callback function. This passage I use the meaning of the association to express, the read file, read the end of the execution of the corresponding operation is placed in a process a

Inside, the execution code B is placed within a thread B, the threading execution coprocessor a,a encounters io blocking, switches thread control, executes the b,b execution end, switches thread control, and executes the coprocessor a. For JS users, the co-process is another form of representation of callbacks.

function Sleep (ms) {

return new Promise ((Resolve,reject) =>settimeout (

() =>resolve (), MS

))

}

(Async function () {

Await Sleep (3000)

Console.log ("Hello")

}())

(Async function () {

Await Sleep (3000)

Console.log ("World")

}())

As you can see, the execution of an async function is to run a process code, and the await keyword is to switch the co-process and execute the code of the other threads after the await.

Func deferprint (str string) {

Time. Sleep (time. SECOND*2)

Fmt. Println (str)

}

Func Main () {

Go deferprint ("Hello")

Go Deferprint ("World")

If the main process does not block, never switch

Time. Sleep (time. SECONDS*2)

}

Golang's go keyword is to put a piece of code in a co-process, thread selection process run, encountered blocking automatically switch the co-operation, but it should be noted that Golang will not be automatically switched because of the end of a process run, must be blocked

Core difference lock mechanism

The Golang process is a Lock.mutex () Unlock () that can be locked, and Nodejs is known to never deadlock or lock it at all.

The problem with no locks is that the resources used are easily modified

such as reading a file, if the file is 0kb I will write a string in, if more than 0kb, I do not do anything. There will be two blocking in the process, the first time to read the file, determine the file size, and the second is write. If there are two functions signed as follows

Async function GetFileSize (filename): number

Async function WriteFile (data,filename): bool

Async function exec () {

Size = await GetFileSize ("./test.txt");

if (size==0) {

await WriteFile ("Hello", "./test.txt")

Console.log ("ok! ")

}

}

If I do the insert operation in the other process after the first judgment, then the order of execution of several functions becomes

A:getfilesize () Check file size, co-path blocking, switch co-process

B: Write file, block, switch co-process

A:getfilesize () Check text size end, can be inserted, execute WriteFile ()

But at this point in the queue there is also a B-process of the insert operation will be performed before a, a-process does not know, for the file or 0kb

If the file is locked,

A:getfilesize () Check file size, co-path blocking, switch co-process

B: Write to file, oh----------------I lock this file, I'll wait for him to release it, switch the co-process

A:getfilesize () complete, insert operation

B: Oh--it's still in use, so I'll wait.

A: It's done, release the lock, I've got nothing to do, cut me out of the queue

B: Process A releases the lock on the file, and now I can write the

Threading Support

Golang to support the lock association, I think it's for multi-threading support. Golang can enable multiple threads to execute the same number of concurrent processes in parallel.

Nodejs is limited to V8 's isolate mechanism and can only be run in a single thread. All code cannot be executed in parallel and cannot handle computationally intensive scenarios.

Switching mechanism

Nodejs use await blocking the process, manually switch thread control, node's process is C + + control, C + + wrote this function can be pushed into the event queue can be encapsulated into the promise process

Golang automatically switch the co-process when the process is blocked, so when writing Golang all the code can write synchronization code, and then use the GO keyword to call, Golang the process is their own rules, all

function must toggle thread control when blocking

Take the return value

The async function in Nodejs is a value that can be returned directly.

Golang can only pass a reference to the channel

Overall Golang and Nodejs scenarios are different. Nodejs suitable for front-end Tinker, with Plug/ejs with EXPRESS/KOA2 from the server HTTP request data and then fill in the template engine

Golang similar to small C + +, the biggest highlight is the use of the co-process management multithreading

For the language, you should not pick the side station, but still hold a wave of C #, in addition to running other crush on. NET all other opponents

About the process: different Nodejs and Golang

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.