JS Promises 是类似处理器的并行性吗? [英] Are JS Promises, processor-like parallelism?

查看:32
本文介绍了JS Promises 是类似处理器的并行性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 JS Promises 并且因为它们通常

I was reading about the JS Promises and since they are generally

对于异步成功/失败非常有用,因为您对某项可用的确切时间不太感兴趣,而更感兴趣的是对结果做出反应.

extremely useful for async success/failure, because you're less interested in the exact time something became available, and more interested in reacting to the outcome.

我不能不记得处理器流水线"他们的指令执行以获得一定程度的并行性的方式.总之

I couldn't not to remember about the way processors "pipeline" their instruction execution in order to get a degree of parallelism. In short

处理器芯片可以并行执行指令.但是,如何将打算串行执行的机器指令流分成有意义的并行流?处理器执行此操作的方式非常聪明且非常复杂,但本质上,它们通常会在一个单独的执行管道中有效地猜测"操作的结果,并根据该猜测的假设开始执行.如果后来发现猜测是错误的,则基于它的流的结果将被简单地转储.没有获得——但也没有损失.然而,如果猜测是正确的,那么处理器在单次执行流的时间内完成了两倍的工作——因此对于那个咒语,它的运行速度是原来的两倍.

Processor chips can execute instructions in parallel. But how do you take a stream of machine instructions intended to be executed serially and divide them up into parallel streams that make sense? The way processors do this is very clever and quite complex, but in essence they often come to points where they effectively "guess" the result of an operation in a separate execution pipeline and start executing on the assumption of that guess. If the guess is later found to be wrong, the results of the stream that was based on it are simply dumped. There has been no gain—but no loss either. However, if the guess was good, the processor has just done twice as much work in the time it would take a single stream of execution—so for that spell, it was running twice as fast.

我的问题是 - JS Promise 是否实现了相同的概念?

My question is - does JS Promises implement the same concept?

推荐答案

不,这不是并行处理,因为 promises 可以在单个线程上并且没有内置任务并行化的概念,它们不会破坏问题分解为并行子任务等.它们只是一种响应和管理异步事件的方法.

No, this is not parallel processing as promises can be on a single thread and there is no concept of task parallelization built into them, they arent going to break a problem down into parallel subtasks etc. They are solely a method to respond to and manage asynchronous events.

我能想到的最好的模拟并行性的模拟将要求用户将他们的问题分解为可并行化的任务,然后将每个调用传递给 Promise.All 类型函数,该函数将使用 webworkers 来处理调用.很可能是无用的练习,因为您基本上只是用承诺装饰网络工作者.在这种情况下,最好建议您利用像 parallel.js 这样的库.

The best mock I could think of to mimic parallelism would require the user to break their problem down into parallizable tasks and then pass each call to a Promise.All type function which would use webworkers to handle the invocations. Most likely a useless exercise as you are basically just decorating webworkers with promises. You'd be much better advised to leverage a library like parallel.js in cases like this.

这篇关于JS Promises 是类似处理器的并行性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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