如何进行工人之间的沟通? [英] How to do worker-to-worker communication?

查看:124
本文介绍了如何进行工人之间的沟通?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对网络工作者进行试验,并且很想知道他们会如何处理不同寻常的问题。因此,我实施了 Connaway的生命游戏。 (为了获得一点乐趣而不是做模糊,或者其他的问题,但是在这种情况下问题会是一样的)。



目前,我有一个网络工作者执行迭代并发布新的ImageData以便UI线程放置在我的画布中。



然而,我的实验并没有结束,因为我有几个CPU可用,并且想要并行化我的应用程序。

因此,我首先简单地将数据分成两部分,中间是两部分,每部分分别处理一半。问题当然是分裂。工作者A需要来自工人B的一列像素,反之亦然。现在,我可以明确地解决这个问题,让我的UI线程将这个列给工人,但是如果我的线程可以直接将它们传递给eachother,那会更好。

进一步分解时,每个工作人员只需跟踪其相邻的工作人员,UI线程只负责更新UI(因为它应该是) 。

我的问题是,我没有看到如何实现这种工人对工作者的沟通。我尝试通过初始化postMessage将邻居交给彼此,但这会复制我的工作人员,而不是传递参考,幸运的是Chrome告诫我不可能。

 未捕获错误:DATA_CLONE_ERR:DOM例外25 

最后我看到了有一种叫做 SharedWorker 的东西。这是我应该看看,还是有办法使用 Worker 来解决我的问题?

解决方案

您应该可以使用频道讯息

  var channel = new MessageChannel(); 
worker1.postMessage({code:port},[channel.port1]);
worker2.postMessage({code:port},[channel.port2]);

然后在您的工作线程中:

  var xWorkerPort; 
onmessage = function(event){
if(event.data.code ==port){
xWorkerPort = event.ports [0];
xWorkerPort.onmessage = function(event){/ * do stuff * /};




$ b $ p
$ b

没有太多的文档,但你可以试试< a href =http://msdn.microsoft.com/en-gb/library/hh673525.aspx>此MS摘要开始。


I'm experimenting with web workers, and was wondering how well they would deal with embarassingly parallell problems. I therefore implemented Connaway's Game of Life. (To have a bit more fun than doing a blur, or something. The problems would be the same in that case however.)

At the moment I have one web worker performing iterations and posting back new ImageData for the UI thread to place in my canvas. Works nicely.

My experiment doesn't end there however, cause I have several CPU's available and would like to parallellize my application.

So, to start off simply I split my data in two, down the middle, and make two workers each dealing with a half each. The problem is of course the split. Worker A needs one column of pixels from worker B and vice versa. Now, I can clearly fix this by letting my UI-thread give that column down to the workers, but it would be much better if my threads could pass them to eachother directly.

When splitting further, each worker would only have to keep track of it's neighbouring workers, and the UI thread would only be responsible for updating the UI (as it should be).

My problem is, I don't see how I can achieve this worker-to-worker communication. I tried handing the neighbours to eachother by way of an initialization postMessage, but that would copy my worker rather than hand down a reference, which luckily chrome warned me about being impossible.

Uncaught Error: DATA_CLONE_ERR: DOM Exception 25

Finally I see that there's something called a SharedWorker. Is this what I should look into, or is there a way to use the Worker that would solve my problem?

解决方案

You should be able to use channel messaging:

var channel = new MessageChannel();
worker1.postMessage({code:"port"}, [channel.port1]);
worker2.postMessage({code:"port"}, [channel.port2]);

Then in your worker threads:

var xWorkerPort;
onmessage = function(event) {
    if (event.data.code == "port") {
        xWorkerPort = event.ports[0];
        xWorkerPort.onmessage = function(event) { /* do stuff */ };
    }
}

There's not much documentation around, but you could try this MS summary to get started.

这篇关于如何进行工人之间的沟通?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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