英文:
is there a way to share a function from the main thread with a web worker?
问题
我看到MDN文档中有DedicatedWorkerGlobalScope
和SharedWorkerGlobalScope
,但没有关于如何使用它的示例。
我想知道是否可以将一个函数从主线程传递给工作线程?一个涉及工作线程中不可用的东西的函数... 例如,像这样的简单内容:
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
andSharedWorkerGlobalScope
in the mdn docs, but no examples on how to use it.
我看到 mdn 文档中有DedicatedWorkerGlobalScope
和SharedWorkerGlobalScope
,但没有关于如何使用它的示例。
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论