有办法将主线程中的函数与Web Worker共享吗?

huangapple go评论59阅读模式
英文:

is there a way to share a function from the main thread with a web worker?

问题

我看到MDN文档中有DedicatedWorkerGlobalScopeSharedWorkerGlobalScope,但没有关于如何使用它的示例。

我想知道是否可以将一个函数从主线程传递给工作线程?一个涉及工作线程中不可用的东西的函数... 例如,像这样的简单内容:

f = (msg) => { console.log(`来自主线程的问候!工作线程说:${msg}`); };

然后工作线程调用它将使日志记录在浏览器中从工作线程发生。是否可以使用这些全局范围API来实现这样的功能?

英文:

I see that there is a DedicatedWorkerGlobalScope and SharedWorkerGlobalScope in the mdn docs, but no examples on how to use it.

I am wondering is it possible to pass a function from the main thread to the worker? A function that involves things that aren't available in the worker's world.. For example something simple like:

f = (msg) => { console.log(`hello from main thread! worker thread says: ${msg}`); };

And then the worker calling that would make it so that logging to the browser happens from the worker thread. Is something like this possible to do with those global scope apis?

答案1

得分: 4

Is there a way to share a function from the main thread with a web worker?
没有。唯一可以共享的数据是二进制数据:buffers。函数甚至无法被 传递结构克隆发送到工作线程,发送代码的唯一方法是作为字符串。

I see that there is a DedicatedWorkerGlobalScope and SharedWorkerGlobalScope in the mdn docs, but no examples on how to use it.
我看到 mdn 文档中有 DedicatedWorkerGlobalScopeSharedWorkerGlobalScope,但没有关于如何使用它的示例。

Is something like this possible to do with those global scope apis?
我认为你可能对这里的“全局范围”和“API”有些困惑。这不是用来创建全局范围或其他内容的 API,它只是文档,列出了在专用或共享 Web Worker 中执行的代码的全局范围中可用的全局变量和方法。它们本质上是 Window 接口 在工作线程中的等效物。

英文:

> Is there a way to share a function from the main thread with a web worker?

No. The only data that can be shared is binary: buffers. Functions cannot even be transferred or structurally cloned to send them to a worker, the only way to send code is as a string.

> I see that there is a DedicatedWorkerGlobalScope and SharedWorkerGlobalScope in the mdn docs, but no examples on how to use it.

There's plenty of examples, look into the pages of all the listed methods!

> Is something like this possible to do with those global scope apis?

I think you are confused what "global scope" and "API" mean here. This is not an API to create global scopes or anything, it's just the documentation of which global variables and methods are available in the global scope to code executing in a dedicated or shared web worker. They're essentially the worker equivalents of the Window interface.

答案2

得分: 0

只能传递缓冲区和基本类型,如字符串给Web Worker。如果你信任你的函数并且它不是闭包,你可以将函数转为字符串,传递给Web Worker,然后在Web Worker 中创建该函数:

你的主 JavaScript 代码部分:

const f1 = (msg) => { console.log(`hello from main thread! worker thread says: ${msg}`); };
const f1String = f1.toString();

f1String 传递给 Web Worker。在 Web Worker 中:

let f2;
eval('f2 =' + f1String);
let msg = 'Hi there';
f2(); // 'hello from main thread! worker thread says: Hi there'

更新 1 关于基于闭包的内容,基于 Bergi 的评论。

英文:

You can't pass along a function to a web-worker, only buffers and basic types like strings.

If you trust your function and it is not a closure you can stringify the function, pass it to the web-worker, and create the function in the web-worker:

You main JavaScript:

const f1 = (msg) => { console.log(`hello from main thread! worker thread says: ${msg}`); };
const f1String = f1.toString();

Pass f1String to the web-worker. In web-worker:

let f2;
eval('f2 =' + f1String);
let msg = 'Hi there';
f2(); // 'hello from main thread! worker thread says: Hi there'

UPDATE 1 on closure based on Bergi's comment.

huangapple
  • 本文由 发表于 2023年2月6日 06:57:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定