gpt4 book ai didi

javascript - JS Promises 是类似处理器的并行性吗?

转载 作者:行者123 更新时间:2023-11-28 11:34:41 24 4
gpt4 key购买 nike

我正在阅读有关 JS Promises 的内容因为它们通常是

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.

我无法不记得处理器如何“流水线”执行指令以获得一定程度的并行性。简而言之

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 Promises 是否实现了相同的概念?

最佳答案

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

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

关于javascript - JS Promises 是类似处理器的并行性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26347305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com