Async.js - 平行真的水货? [英] Async.js - Is parallel really parallel?

查看:193
本文介绍了Async.js - 平行真的水货?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我迄今了解:JavaScript是单线程的。如果你推迟一些过程的执行,您只需安排它(排队的话)来运行下一次线程是免费的。但Async.js定义了两种方法:异步并行::功放&;异步:: parallelLimit ,我引用:


  • 并行(任务,[回调])


  

运行并行功能的阵列,无需等待,直到previous功能已经完成。如果有任何的功能传递一个错误的回调...



  • parallelLimit(任务,限制,[回调])


  

平行只有任务是在平行于最大的随时执行限制的任务执行的相同。


至于我的英语,了解当你说:并行执行任务的是指在同一时间做他们 - 同时

如何可能Async.js在单个线程并行执行任务吗?
我失去了一些东西。


解决方案

  

如何可能Async.js在单个线程并行执行任务吗?我失去了一些东西。


平行同时运行所有任务。所以,如果您的任务包含I / O调用(例如查询数据库),就好像他们已经在并行处理完毕后,才会出现。


  

这是怎么在单个线程启用?这就是我不能让感。


Node.js的是非阻塞的。因此,而不是处理所有任务并行,它从一个任务转换到另一种。因此,当第一个任务,使I / O调用使自己闲着,Node.js的简单切换到另一个处理的。

I / O任务花费了大多数的处理时间等待I / O调用的结果。在阻塞语言如Java,这样的任务块的线程在等待结果。但Node.js的利用它的时间来处理,而不是等待另一个任务。


  

因此​​,这意味着,如果每个任务的内部处理是异步如果它们中的任何人已完成或直到所有已完成它们的位线被授予的这个任务不管每个位


是的,它几乎就像你说的。 Node.js的开始处理第一个任务,直至暂停,并执行I / O调用。在那一刻,叶的Node.js它,并赋予它的主线程其它任务。所以,你可能会说,该线程被授予依次在每个活动任务。

As I have understood so far: Javascript is single threaded. If you defer the execution of some procedure, you just schedule it (queue it) to be run next time the thread is free. But Async.js defines two methods: Async::parallel & Async::parallelLimit, and I quote:

  • parallel(tasks, [callback])

Run an array of functions in parallel, without waiting until the previous function has completed. If any of the functions pass an error to its callback...

  • parallelLimit(tasks, limit, [callback])

The same as parallel only the tasks are executed in parallel with a maximum of "limit" tasks executing at any time.

As far as to my understanding of English, when you say: "doing tasks in parallel" means doing them at the same time - simultaneously.

How may Async.js execute tasks in parallel in a single thread? Am I missing something.

解决方案

How may Async.js execute tasks in parallel in a single thread? Am I missing something.

parallel runs all its tasks simultaneously. So if your tasks contain I/O calls (e.g. querying DB), they'll appear as if they've been processed in parallel.

how is this enabled in a single thread?! that is what I could not make sense of.

Node.js is non-blocking. So instead of handling all tasks in parallel, it switches from one task to another. So when the first task makes I/O call making itself idle, Node.js simply switches to processing another one.

I/O tasks spent most of its processing time waiting for the result of the I/O call. In blocking languages like Java, such a task blocks its thread while it waits for the results. But Node.js utilizes it's time to process another tasks instead of waiting.

so that means that if the inner processing of each task is asynchronous the thread is granted to each bit of this tasks regardless if anyone of them has finished or not until all have finished their bits?

Yes, it's almost as you said. Node.js starts processing the first task until it pauses to do an I/O call. At that moment, Node.js leaves it and grants its main thread to another task. So you may say that the thread is granted to each active task in turn.

这篇关于Async.js - 平行真的水货?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆