AJ++,
Also, if you get more than 3 levels deep of nesting, you should
probably split up that function. If you do not need a closure above
your current function, why not make the function outside of the
nesting structure to make it reusable (if done right can save quite a
bit of memory and construction time for hot areas).
Async evented coding often trips people up because they want to write
*straight forward* code. Threads/Coroutines(fibers) have taught many
people that code can always be done in a linear fashion and it can
hide the blocking/wait looping nature of code. This unfortunately
comes at a terrible price... wait loops for every thread at times.
Instead of thinking of your code as a waterfall / list (blocking/
threaded), think of it as a tree / errands list. Since topics like
these come up quite often on the mailing list I would like to go into
greater detail the strengths and weaknesses I find in them. Note:
*Node.js encourages the actor models*.
Evented Async (Single Actor Model):
You can gather up all the errands (callbacks/handlers) you need and do
them when it is possible for each, this sometimes means you must
complete one before the other (cant pick up groceries before the bank
etc.), which you might want to split up errands if you need 8hrs doing
a single one so that you can do others.
* encourages splitting up of temporal logic, easily visible splitting
up of code branching
* you must know when your program is blocking and the requirements of
each errand (and if you need to split it up), you lose your stack
trace (you'll get used to it, just avoid anonymous functions)
Worker Async (Multiple Actor Model):
You gather up all of the errands, the head of the household tells
however many people (number of workers) to go do those errands with a
specific one given to each. Once they are done and return the head of
household crosses them off of the list.
* scales well, still no need for semaphores, still quite clear of what
code is being run where, workers can often be reused without
respawning
* workers (generally as processes) have a cost for creation/memory
consumption/cpu/etc. workers may fail for some reason (unlikely if
done right) and backups of their entry parameters should be kept.
Threaded Async:
Stopping halfway done to go to another errand (like a thread), and I
think we can agree that is a little odd if you just talk about what
you are doing. And on top of that, lets gather up all the resources we
need to perform all the errands we are on and keep them with us at all
times, even when we are not working on them.
* hides most knowledge of when to do what, except when you need to use
shared data, or communicate between threads, scales well if threads
have no shared resources (unlikely), threads can often be reused
without spawning/despawning
* wasteful memory and computation wise, and you need semaphores for
any shared data (shudder), threads are sometimes left for reuse and
never are
Cooperative Async:
And lastly, cooperative programming (coroutines) it would be like the
async evented approach except you would have to plan out every step in
advance and decided sometimes you don't want to finish an errand
because another one may be able to be finished.
* much of the knowledge of the scheduler is hidden with no need for
semaphore madness
* can be extremely hard to schedule some things in order, stack /
context regeneration has a cost
Cheers,
Bradley
On Oct 29, 9:32 am, AJ ONeal <
coola...@gmail.com> wrote:
> There is a major advantage to the asynchronous style, btw. It helps you to
> know where your functions should naturally be splitting up.
>
> Less temptation to write horrendously long and difficult to read functions.
>
> Also, your data becomes more clearly divided into the groups it belongs.
>
> Read "how to think about oo" by Google testing blog. It's helped me to
> better organise my modules and such.
>
> AJ
>
> Sent from my Google Android
>
> On Oct 29, 2010 1:52 AM, "rtweed" <
rob.tw...@gmail.com> wrote:> Came across this yesterday:
>
>
http://blogs.msdn.com/b/somasegar/archive/2010/10/28/making-asynchron...
>
> > Would something similar be possible in Node.js?
>
> > Rob
>
> > --
> > You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> > To post to this group, send email to
nod...@googlegroups.com.
> > To unsubscribe from this group, send email to
>
>
nodejs+un...@googlegroups.com <
nodejs%2Bunsu...@googlegroups.com>.> For more options, visit this group at
>
>
http://groups.google.com/group/nodejs?hl=en.
>
>
>
>
>
>
>
>