Async.js - 并行真的是并行的吗? [英] Async.js - Is parallel really parallel?

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

问题描述

据我目前所知:Javascript 是单线程的.如果您推迟某个过程的执行,您只需安排它(排队)在下次线程空闲时运行.但是 Async.js 定义了两个方法: Async::parallel &Async::parallelLimit,我引用:

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:

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

并行运行一组函数,无需等到前一个函数完成.如果任何函数向其回调传递错误...

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.

    Async.js 如何在单线程中并行执行任务?我是不是错过了什么.

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

    推荐答案

    Async.js 如何在单线程中并行执行任务?我是不是遗漏了什么.

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

    parallel 同时运行它的所有任务.因此,如果您的任务包含 I/O 调用(例如查询数据库),它们将看起来好像是并行处理的.

    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 是非阻塞的.因此,它不是并行处理所有任务,而是从一项任务切换到另一项任务.因此,当第一个任务进行 I/O 调用使其自身空闲时,Node.js 只需切换到处理另一个任务.

    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 任务的大部分处理时间都用于等待 I/O 调用的结果.在 Java 等阻塞语言中,这样的任务在等待结果时会阻塞其线程.但是 Node.js 利用时间来处理另一个任务,而不是等待.

    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?

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

    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天全站免登陆