简体   繁体   English

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

[英]Sharing variables between web workers? [global variables?]

Is there any way for me to share a variable between two web workers?有什么办法可以让我在两个网络工作者之间共享一个变量? (Web workers are basically threads in Javascript) (Web Worker 基本上是 Javascript 中的线程)

In languages like c# you have:在像 c# 这样的语言中,你有:

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.我知道这是一个不好的例子,但在我的 Javascript 应用程序中,我有一个线程在执行大量计算,可以分布在多个线程中 [因为我有大量数组形式的数据。 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]我发现在两个线程之间“共享”变量的唯一方法是创建一个 Getter/setter [通过原型],然后使用 postMessage/onmessage ......尽管这看起来效率很低 [特别是对于对象,我有为 AFAIK 使用 JSON]

LocalStorage/Database has been taken out of the HTML5 specification because it could result in deadlocks, so that isn't an option [sadly]... LocalStorage/Database 已从 HTML5 规范中删除,因为它可能导致死锁,因此这不是一个选项 [可悲]...

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.我发现的另一种可能性是使用 PHP 实际上有一个 getVariable.php 和 setVariable.php 页面,它们使用 localstorage 来存储整数/字符串......再次,对象 [包括数组/空] 必须转换为JSON...然后是 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据我所知,Javascript 工作线程与主页面线程完全隔离[这就是 Javascript 工作线程无法访问 DOM 元素的原因

Although postMessage works, it is slow.尽管 postMessage 有效,但速度很慢。

Web workers are deliberately shared-nothing -- everything in a worker is completely hidden from other workers and from pages in the browser. Web worker 是故意不共享的——worker 中的所有内容都对其他 worker 和浏览器中的页面完全隐藏。 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.如果有任何方法可以在 worker 之间共享非“原子”值,那么这些值的语义几乎不可能用于可预测的结果。 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.这对开发人员或用户来说都没有好处(尤其是当您考虑到 Web 环境非常适合那些从未听说过线程、锁或消息传递的非程序员进行实验时),因此替代方案是在两者之间不共享状态浏览器中的工作人员或页面。 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.你真的不想要你认为你想要的替代品。

No, but you can send messages to web workers which can be arrays, objects, numbers, strings, booleans, and ImageData or any combination of these.不,但您可以向网络工作者发送消息,这些消息可以是数组、对象、数字、字符串、布尔值和 ImageData 或这些的任意组合。 Web workers can send messages back too.网络工作者也可以发回消息。

There are two options to share data between dedicated workers:有两种方法可以在专用 worker 之间共享数据:

1. Shared Workers 1. 共享工人

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. SharedWorker 接口代表一种特定类型的工作器,可以从多个浏览上下文访问,例如多个窗口、iframe 甚至工作器。

Spawning a Shared Worker in a Dedicated Worker 在专用工作线程中生成共享工作线程

2. Channel Messaging API 2. 频道消息API

The Channel Messaging API allows two separate scripts running in different browsing contexts attached to the same document (eg, 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. Channel Messaging API 允许在附加到同一文档的不同浏览上下文中运行的两个单独脚本(例如,两个 IFrame,或主文档和一个 IFrame,通过 SharedWorker 的两个文档,或两个工作程序)直接通信,在一个之间传递消息另一个通过两端各有一个端口的双向通道(或管道)。

How to call shared worker from the web worker? 如何从网络工作者调用共享工作者?

I recently read about (but have not used), shared workers .我最近阅读了(但没有使用过) 共享的 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). Opera 自带 SharedWorker 支持,仅支持最新的浏览器(Opera 10.6、Chrome 5、Safari 5)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM