在网络工作者之间共享变量? [全局变量?]

发布于 2024-08-21 16:44:44 字数 877 浏览 8 评论 0原文

有什么办法可以让我在两个网络工作者之间共享变量吗? (Web Workers 基本上是 Javascript 中的线程)

在像 c# 这样的语言中,你有:

public static string message = "";
static void Main()
{
 message = "asdf";
 new Thread(mythread).Run();
}
public static void mythread()
{
 Console.WriteLine(message); //outputs "asdf"
}

我知道这是一个坏例子,但是在我的 Javascript 应用程序中,我有一个线程执行繁重的计算,可以分布在多个线程中 [因为我有一个很大的块数组形式的数据。数组的所有元素都是相互独立的。换句话说,我的工作线程不必关心锁定或类似的事情]

我发现在两个线程之间“共享”变量的唯一方法是创建一个 Getter/setter [通过原型],然后使用 postMessage/onmessage...虽然这看起来效率很低[特别是对于对象,据我所知我必须使用 JSON]

LocalStorage/Database 已从 HTML5 规范中删除,因为它可能导致死锁,所以这不是一个选项[悲伤]...

我发现的另一种可能性是使用 PHP 实际上有一个 getVariable.php 和 setVariable.php 页面,它们使用 localstorage 来存储整数/字符串...再次,对象[其中包括数组/ null] 必须转换为 JSON...然后是 JSON.parse()'d。

据我所知,Javascript工作线程与主页面线程完全隔离[这就是为什么Javascript工作线程无法访问DOM元素

虽然postMessage可以工作,但速度很慢。

Is there any way for me to share a variable between two web workers? (Web workers are basically threads in Javascript)

In languages like c# you have:

public static string message = "";
static void Main()
{
 message = "asdf";
 new Thread(mythread).Run();
}
public static void mythread()
{
 Console.WriteLine(message); //outputs "asdf"
}

I know thats a bad example, but in my Javascript application, I have a thread doing heavy computations that can be spread across multiple threads [since I have a big chunk of data in the form of an array. All the elements of the array are independent of each other. In other words, my worker threads don't have to care about locking or anything like that]

I've found the only way to "share" a variable between two threads would be to create a Getter/setter [via prototyping] and then use postMessage/onmessage... although this seems really inefficient [especially with objects, which I have to use JSON for AFAIK]

LocalStorage/Database has been taken out of the HTML5 specification because it could result in deadlocks, so that isn't an option [sadly]...

The other possibility I have found was to use PHP to actually have a getVariable.php and setVariable.php pages, which use localstorage to store ints/strings... once again, Objects [which includes arrays/null] have to be converted to JSON... and then later, JSON.parse()'d.

As far as I know, Javascript worker threads are totally isolated from the main page thread [which is why Javascript worker threads can't access DOM elements

Although postMessage works, it is slow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

離人涙 2024-08-28 16:44:45

Web Worker 故意不共享——Worker 中的所有内容对其他 Worker 和浏览器中的页面完全隐藏。如果有任何方法可以在工作人员之间共享非“原子”值,那么这些值的语义几乎不可能以可预测的结果使用。现在,在某种程度上,可以引入锁作为使用这些值的一种方式——您获取锁,检查并可能修改该值,然后释放锁——但是锁非常棘手使用时,由于通常的故障模式是死锁,因此您可以很容易地“阻塞”浏览器。这对于开发人员或用户来说没有什么好处(尤其当您考虑到 Web 环境非常适合由从未听说过线程、锁或消息传递的非程序员进行实验时),因此另一种方法是在浏览器中的工作程序或页面之间不共享状态。您可以传递消息(可以将其视为“通过线路”序列化给工作线程,然后工作线程根据序列化信息创建自己的原始值副本),而无需解决任何这些问题。

实际上,消息传递是支持并行性而不会让并发问题完全失控的正确方法。正确地安排你的消息传递,你应该拥有与共享状态一样多的权力。你真的不想要你认为你想要的替代方案。

Web workers are deliberately shared-nothing -- everything in a worker is completely hidden from other workers and from pages in the browser. If there were any way to share non-"atomic" values between workers, the semantics of those values would be nearly impossible to use with predictable results. Now, one could introduce locks as a way to use such values, to a certain extent -- you acquire the lock, examine and maybe modify the value, then release the lock -- but locks are very tricky to use, and since the usual failure mode is deadlock you would be able to "brick" the browser pretty easily. That's no good for developers or users (especially when you consider that the web environment is so amenable to experimentation by non-programmers who've never even heard of threads, locks, or message-passing), so the alternative is no state shared between workers or pages in the browser. You can pass messages (which one can think of as being serialized "over the wire" to the worker, which then creates its own copy of the original value based on the serialized information) without having to address any of these problems.

Really, message-passing is the right way to support parallelism without letting the concurrency problems get completely out of control. Orchestrate your message handoffs properly and you should have every bit as much power as if you could share state. You really don't want the alternative you think you want.

七禾 2024-08-28 16:44:45

有两种选项可以在专门的工作人员之间共享数据:

1。 共享工作线程

SharedWorker 接口代表一种特定类型的工作线程
可以从多个浏览上下文访问,例如多个
windows、iframe 甚至工作人员。

在专用工作线程中生成共享工作线程

2 。 频道消息 API

Channel Messaging API 允许两个单独的脚本运行在
附加到同一文档的不同浏览上下文(例如,两个
IFrames,或者主文档和一个IFrame,两个文档通过一个
SharedWorker,或者两个worker)直接通信,传递
通过双向通道(或管道)相互之间传递消息
两端各有一个端口。

如何从 Web Worker 调用共享 Worker?

There are two options to share data between dedicated workers:

1. Shared Workers

The SharedWorker interface represents a specific kind of worker that
can be accessed from several browsing contexts, such as several
windows, iframes or even workers.

Spawning a Shared Worker in a Dedicated Worker

2. Channel Messaging API

The Channel Messaging API allows two separate scripts running in
different browsing contexts attached to the same document (e.g., two
IFrames, or the main document and an IFrame, two documents via a
SharedWorker, or two workers) to communicate directly, passing
messages between one another through two-way channels (or pipes) with
a port at each end.

How to call shared worker from the web worker?

雨夜星沙 2024-08-28 16:44:45

不可以,但您可以向 Web Worker 发送消息,消息可以是数组、对象、数字、字符串、布尔值和 ImageData 或这些的任意组合。网络工作者也可以发回消息。

No, but you can send messages to web workers which can be arrays, objects, numbers, strings, booleans, and ImageData or any combination of these. Web workers can send messages back too.

给妤﹃绝世温柔 2024-08-28 16:44:45

我最近读到(但没有使用过),共享工作者。根据分享工作! Opera 附带 SharedWorker 支持,仅在最新的浏览器(Opera 10.6、Chrome 5、Safari 5)中提供支持。

I recently read about (but have not used), shared workers. According to Share the work! Opera comes with SharedWorker support, support is only in the newest browsers (Opera 10.6, Chrome 5, Safari 5).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文