网络工作者之间共享变量? [全局变量?] [英] Sharing variables between web workers? [global variables?]

查看:102
本文介绍了网络工作者之间共享变量? [全局变量?]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种方法可以在两个网络工作者之间分享变量吗? (Web工作人员基本上是Javascript中的线程)



在像c#这样的语言中,您可以:

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

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



我发现了共享两个变量之间的唯一方法线程将创建一个Getter / setter [通过原型],然后使用postMessage / onmessage ...虽然这似乎是非常低效[特别是对象,我必须使用JSON for AFAIK]



LocalStorage / Database已经被取出HTML5规范,因为它可能导致死锁,所以这不是一个选项[可悲] ...

我发现的另一种可能性是使用PHP实际上有一个getVariable.php和setVariable.php页面,这些页面使用localstorage存储ints / strings ...再一次,Object [包含arrays / null]必须是转换为JSON ...然后JSON.parse()'d。



据我所知,Javascript工作线程完全与主页面线程隔离[这就是为什么Javascript工作线程不能访问DOM元素的原因nts



尽管postMessage有效,但速度很慢。



谢谢!

解决方案

- 工作人员中的所有内容都完全隐藏在浏览器中的其他工作人员和页面中。如果有任何方法可以在工人之间共享非原子价值,那么这些价值的语义几乎不可能用于可预测的结果。现在,一个可以引入锁作为使用这些值的一种方式,在某种程度上 - 您获取锁,检查并可能修改该值,然后释放锁 - 但锁非常棘手使用,而且由于通常的失败模式是死锁,所以你可以很容易地浏览浏览器。这对开发人员或用户来说并不好(特别是当你认为web环境非常适合那些从未听说过线程,锁或消息传递的非程序员进行实验时),所以另一种选择是在工作人员或浏览器中的页面之间不共享状态。你可以传递消息(人们可以认为这些消息是通过序列化的方式传递给worker,然后worker根据序列化信息创建自己的原始值副本),而不必解决任何这些问题。



真的,消息传递是支持并行性而不会让并发问题完全失控的正确方法。协调好你的消息传递,你应该拥有尽可能多的权力,就像你可以共享状态一样。你真的不想要你认为你想要的替代品。


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.

Thanks!

解决方案

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.

这篇关于网络工作者之间共享变量? [全局变量?]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆