Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What does asynchronous IO really come down to on Windows (ie like NodeJS, EventMachine, etc)?

What does asynchronous IO really come down to on Windows (ie like NodeJS, EventMachine, etc)?

Problem

I have done a great deal of research into this topic of an 'event loop' that does Asynchronous IO using a single thread. My understanding is that, for instance, Nodejs code runs in a single-threaded environment, but does pass off jobs to parallel threads behind-the-scenes. Meaning everything runs concurrently except the code and the interpreter, which just passes off jobs and manages callbacks, in a single-threaded manner.

My question is with regards to IO in Windows and ThreadPool threading specifically, especially file IO and database reads/writes. Typically in a .NET environment that uses multi-threading you might see this:

lock(sync)
{
          // do some IO
}

To protect certain functions from concurrency issues, like two threads trying to save records to the same sql table or file.

My question is two-fold,

1) When nodeJS says it does 'Asynchronous IO' does it mean that it actually writes to a database or file concurrently? I think that would be up to the underlying device / operating system, and not nodeJS, but I'm trying to clarify.

2) If some IO operations have to be or at least should be atomic (not done concurrently), how is 'Asynchronous IO' of any help? Couldn't it be wasteful to spawn threads to do IO if you just end up having to lock() alot of those operations anyway?

Problem courtesy of: Sean Thoman

Solution

When nodeJS says it does 'Asynchronous IO' does it mean that it actually writes to a database or file concurrently? I think that would be up to the underlying device / operating system, and not nodeJS, but I'm trying to clarify.

It's up to nodeJS whether it does other things while it is writing to a database or file. The concurrency does not refer to performing the same type of operation on the same target. It refers to performing different types of operations or operations on different targets.

If some IO operations have to be or at least should be atomic (not done concurrently), how is 'Asynchronous IO' of any help? Couldn't it be wasteful to spawn threads to do IO if you just end up having to lock() alot of those operations anyway?

Again, same response. Only if it's the same type of operation on the same target will the locks conflict. Otherwise, if they are requesting locks, they'll request different locks and won't get in each other's way.

The implementation specifics depend on the type of operation. For example, a thread can take ownership of a particular resource and process operations for that resource until it runs out of things to do. While it's handling that resource, other operations on that same resource can be dispatched to that very thread. That way, you don't start a thread just to wait for another thread to finish just to force a pointless context switch.

Solution courtesy of: David Schwartz

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

What does asynchronous IO really come down to on Windows (ie like NodeJS, EventMachine, etc)?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×