开发者

What is the difference between asynchronous I/O and asynchronous function?

开发者 https://www.devze.com 2023-04-09 20:32 出处:网络
Node.js, is an asynchronous I/O. what does this actually mean? Is there different between I create an async function by spawning another thread to do the process?

Node.js, is an asynchronous I/O. what does this actually mean?

Is there different between I create an async function by spawning another thread to do the process?

e.g.

void asyncfuntion(){
    Thread apple = new Thread(){
       public void run(){
           ...do stuff
       }
    }
    apple.开发者_运维问答start()
}

If there is difference, can I do a asynchronous I/O in javascript?


Asynchronous I/O

Asynchronous I/O (from Wikipedia)

Asynchronous I/O, or non-blocking I/O, is a form of input/output processing that permits other processing to continue before the transmission has finished.

What this means is, if a process wants to do a read() or write(), in a synchronous call, the process would have to wait until the hardware finishes the physical I/O so that it can be informed of the success/failure of the I/O operation.

On asynchronous mode, once the process issues a read/write I/O asynchronously, the system calls is returned immediately once the I/O has been passed down to the hardware or queued in the OS/VM. Thus the execution of the process isn't blocked (hence why it's called non-blocking I/O) since it doesn't need to wait for the result from the system call, it will receive the result later.

Asynchronous Function

Asynchronous functions is a function that returns the data back to the caller by a means of event handler (or callback functions). The callback function can be called at any time (depending on how long it takes the asynchronous function to complete). This is unlike the synchronous function, which will execute its instructions before returning a value.

...can I do a asynchronous I/O in java?

Yes, Java NIO provides non-blocking I/O support via Selector's. Also, Apache MINA, is a networking framework that also includes non-blocking I/O. A related SO question answers that question.


There are several great articles regarding asynchronous code in node.js:

  • Asynchronous Code Design with Node.js
  • Understanding Event-driven Programming
  • Understanding event loops and writing great code for Node.js


In addition to @The Elite Gentleman's answer, node doesn't spawn threads for asynchronous I/O functions. Everything under node runs in a single threaded event loop. That is why it is really important to avoid synchronized versions of some I/O functions unless absolutely necessary, like fs.readSync

You may read this excellent blog post for some insight: http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/


I was investigating the same question since this async IO pattern was very new to me. I found this talk on infoq.com which made me very happy. The guy explains very well where async IO actually resides (the OS -> the kernel) and how it is embedded in node.js as the primary idiom for doing IO. Enjoy!

http://www.infoq.com/presentations/Nodejs-Asynchronous-IO-for-Fun-and-Profit


node.js enables a programmer to do asynchronous IO by forcing the usage of callbacks. Now callbacks are similar to the old asynchronous functions that we have been using for a long time to handle DOM events in javascript! e.g.

asyncIOReadFile(fileName, asynFuncReadTheActualContentsAndDoSomethingWithThem);
console.log('IDontKnowWhatIsThereInTheFileYet')
function asynFuncReadTheActualContentsAndDoSomethingWithThem(fileContents) {
    console.log('Now I know The File Contents' + fileContents)
}
//Generally the output of the above program will be following
'IDontKnowWhatIsThereInTheFileYet'
//after quite a bit of time
'Now I know The File Contents somebinarystuff'


From https://en.wikipedia.org/wiki/Asynchronous_I/O

Synchronous blocking I/O

A simple approach to I/O would be to start the access and then wait for it to complete.

Would block the progress of a program while the communication is in progress, leaving system resources idle.

Asynchronous I/O

Alternatively, it is possible to start the communication and then perform processing that does not require that the I/O be completed.

Any task that depends on the I/O having completed ... still needs to wait for the I/O operation to complete, and thus is still blocked,

but other processing that does not have a dependency on the I/O operation can continue.

Example:

https://en.wikipedia.org/wiki/Node.js

Node.js has an event-driven architecture capable of asynchronous I/O

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号