JavaScript中的“WebWorkers”概念是否类似于异步函数? [英] Is `WebWorkers` concept in JavaScript similar to asynchronous functions?

查看:123
本文介绍了JavaScript中的“WebWorkers”概念是否类似于异步函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我在C#中开发了很多,现在我正在开发3D网络项目,现在最有用的语言是JavaScript。

First I have developed much in C#, now I'm working on 3D web project and now the most usable language is JavaScript at the moment.

在C#中直到在新的C#规范中有新的关键字 async / await 有一种方法可以通过以下方式进行异步调用:

In C# until there becomes the new keywords async/await in new C# specification there was a way to make asynchronous calls by using:


  • 委托

  • 开始/结束功能,如:BeginInvoke,EndInvoke

  • IAsync接口

对于JS ...现在我需要使用一些并行计算并且真的需要一些东西,这类似于异步工作和某些锁定模型一样信号量,互斥量或其他..

As for JS... Right now I have a need to use some parallel computations and really need a stuff, which is similar to asynchronous work with some locking models like Semaphore, Mutex or other..

async / await ...我已经尝试了promises概念,它在jQuery中实现了延迟的承诺:

As for async/await... I have tried the promises concept, which is implemented in jQuery with its deferred promise:

  • http://api.jquery.com/deferred.promise/
  • http://api.jquery.com/category/deferred-object/

它仍然是我在C#中的异步/等待概念:

It remains me async/await concept in C#:

http://msdn.microsoft.com/en-us/library/hh191443.aspx

但是我我们也发现了这样的概念: WebWorkers

But I've also have found such concept as WebWorkers:

https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers

当我第一次阅读它时,我认为它也可以是一个解决方案,除了promises模式,但是如果从实现的角度来看,我理解WebWorkers正在从其他方面启动线程比主页面执行线程和函数不是真正异步的,它们只是带有一个选项的回调,它们已被添加到 Wor ker()实例,可以在主页面线程中使用,我是对的吗?

When I first read it, I think it could be also a solution except promises pattern, but if to look from the point of implementing I understand WebWorkers are launching from other threads than a main page execution thread and the functions aren't really asynchronous, they're just callbacks with one option, that they have been added to the Worker() instance, which can be used in main page thread, am I right?

所以......我想知道,我怎么能还在JavaScript中实现类似于信号量的东西?

So... I wonder, how can I also implement something similar to the Semaphore in JavaScript?

谢谢!

更新#1 (更多回复 Doge ):

让我来形容一个简单的例子,有一个应用程序,我正在开发。
我已经使用jQuery延迟对象一件来等待我收到的所有纹理图像,我等待

Let me describe you a simple example, there is an application, which I'm developing. I already using the jQuery deferred object for one thing to await all the texture images I've received, which I was awaiting for.

链接是下一个: http://bit.ly/O2dZmQ

这是 webgl 应用程序, three.js 库,正在构建真实数据的房子(ps:不看代码,我知道它不好:)我最近才了解js编程的原型方法:)首先我太习惯了C#及其范例) 。

It's an webgl application with the three.js library, which is building houses on real data (ps: don't look into code, I know it's not good :) I only recently understand the prototype way of programming in js :) first I was too used to C# and its paradigms ).

所以...我有这样的任务。我必须等待所有纹理从AJAX加载,然后才将它们设置为网格纹理。

So... I have such a task. I must wait when all the textures will be loaded from AJAX and only then I'm setting them as textures for meshes.

现在......当我创建这个问题,我考虑重新开发源代码并考虑WebWorkers使用。

Right now... When I've created this question, I thought about redeveloping the source code and thought about WebWorkers use.

我首先想到的是,我想做什么以及我在开发时做了些什么C#中的WPF / Silverlight应用程序。

What have I think first, which I want to do and what I've done when developed WPF/Silverlight application in C#.

我已经完成了 Worker 的另一个实例,它将异步检查我上面描述的任务。

I've done another instance of Worker, which will check asynchronously the task I've described above.

我做了一个非常小而简单的例子,我想用它,但是有一个失败。

And I have done a very tiny and simple example which I want to use, but have a fail.

当我看到 WebWorkers 时,如果我想将其发送给工作人员,则不接受对象。好吧......

As I saw WebWorkers don't accept objects if I want to send it to worker. Alright...

传递Web对象的对象

有一个 JSON.stringify()方法......但是我看到另一件事... JSON.stringify()无法将对象解析为字符串,其中循环引用

There is a JSON.stringify() method... But I've see another thing... JSON.stringify() can't parse an object to string where the are circular references.

Chrome sendrequest错误:TypeError:转换循环结构为JSON

真的......这是相当令人失望的...因为如果有C#甚至是C ++那么 a在实例之间交换某些对象的问题......有些事情可以通过一些重新解释转换或其他东西来完成......即使在异步工作中,甚至在不同的线程之间交换对象也不是问题......

Truly... It's rather disappointing... Because if there is C# or even C++ it's NOT a problem to exchange between instances some objects... Some things could be done with some reinterpret casts or other stuff... And it's not a problem to exchange objects even between different threads even in asynchronous work...

所以...为了我的目标......什么是最好的解决方案?保留deffered / promises模式而不是使用 WebWorkers

So... For the my aim... What is the best solution? Keep the deffered/promises pattern and not to use WebWorkers?

微小的来源,不是完整的应用程序,只是为了一个小例子,什么我想这样做:

The tiny source, not full application, but just for a small example, what I want to do:

  • http://pastebin.com/5ernFNme ( need HTML for JS, which is below )
  • http://pastebin.com/ELcw7SuE ( JS main logic )
  • http://pastebin.com/PuHrhW8n ( WebWorker, which I suppose to use as for separate checker )

小样本的纹理:

  • http://s14.postimg.org/wqm0xb2ep/box.jpg
  • http://s27.postimg.org/nc77umytv/box2.jpg

可在此处找到缩小的three.js:

Minified three.js could be found here:

  • https://github.com/mrdoob/three.js/blob/master/build/three.min.js

推荐答案

了解JavaScript 没有线程非常重要。它有一个事件循环,在它们进入时逐个执行事件。

It's important to understand that JavaScript has no threads. It has an event loop that executes events, one-by-one, as they come in.

这样做的结果是,如果你有一个需要一段时间的过程你阻止了所有的执行。如果还需要JavaScript来做UI响应用户事件或动画等内容,那就有点麻烦了。你可以尝试将你的过程分成多个事件来保持事件循环顺利运行,但这并不总是那么容易。

The consequence of this is that if you have a process that takes a while you're blocking all execution. If JavaScript is also needed to do UI stuff like responding to user events or animations, it's a bit of snag. You could try to split up your process into multiple events to keep the event loop running smoothly but that's not always easy to do.

这是工人来吧。工人用自己的线程运行。为避免与线程相关的问题,工作人员不共享内存

This is where Workers come it. Workers run in their own thread. To avoid issues related to threads the workers don't share memory.

您可以通过发送和接收消息与工作人员进行通信。消息反过来来自事件循环,因此您不需要信号量或任何同步线程的东西。如果你的工作者控制器是JavaScript,那么从来没有任何原子性问题。

You can communicate with a worker by sending and receiving messages. The messages in turn come in the event loop, so you don't need semaphores or anything that synchronizes threads. If your worker controller is in JavaScript there are never any atomicity issues.

如果你的工人很简单输入 - >输出工作人员然后你可以完全拍打Promise图层。请记住,Promises本身不会添加线程或异步。

If your workers are simple input->output workers then you can totally slap a Promise layer on top of that. Just keep in mind that Promises themselves don't add threads or asynchronousness.

您只能发送Workers消息,即:字符串。你不能发送它们的对象,当然也不能发送可能引用其他对象的对象,因为:内存问题。

You can only send Workers messages, that is: strings. You can't send them objects, and certainly not objects that might have references to other objects because again: memory issues.

如果我查看你的用例,我想是的只有你可能希望工作人员利用大多数CPU现在拥有的多个内核的原因。

If I look at your use case I guess the only reason why you might want the Workers is to take advantage of multiple cores that most CPUs have nowadays.

我认为你正在做的是将图像作为纹理加载到你的帆布,这是需要花费大量时间的?没有好方法在这里使用Workers,因为Worker需要对画布的引用,而这种情况并没有发生。

I think what you're doing is loading images as textures into your canvas, and that's what takes up a lot of time? There is no good way to use Workers here since the Worker would need a reference to the canvas, and that's not happening.

现在,如果相反,你需要对它进行处理纹理以某种方式转换它们可以使用Workers。将图像数据作为二进制字符串发送,可能是base64_encoded,进行转换并将其发回。如果您的图像很大,序列化也会占用相当大的CPU时间,因此您的里程可能会有所不同。

Now if instead you needed to do processing on the textures to transform them in some way you could use Workers. Send the image data as a binary string, possibly base64_encoded, do the conversion and send it back. If your image is large the serialization will also take up a fair chunk of CPU time so your mileage may vary.

从我可以告诉您的资源加载非常快似乎不是CPU瓶颈。所以我不知道你是否真的需要工人。

From what I can tell your resources load pretty quick and there doesn't seem to be a CPU bottleneck. So I don't know if you really need the Workers.

这篇关于JavaScript中的“WebWorkers”概念是否类似于异步函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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