意见关于网络工作者同步请求 [英] Opinion about synchronous requests in web workers

查看:104
本文介绍了意见关于网络工作者同步请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你怎么看待这个问题。推荐使用在网络工作者同步请求(XMLHtt prequest)?我能找到什么问题?

I want to know what do you think about this. Is recommended to use synchronous requests (XMLHttpRequest) in a web worker? What problems can I find?

我一直在测试这个在我的应用程序,我还没有发现任何问题。但我怕,因为老的经验与jQuery和AJAX这一SYNCHRONUS行为。 我的应用程序中获取数据的一个很大的数目从几个数据库中的表,而这需要时间。对于每串从表中检索数据,我需要立即进行处理,以不耽误整个事情太多了。同时,用户与浏览器交互,因此它可以被阻止,我认为网络工作者将正常工作。 你的事情,这是一个很好的解决方案?或者我应该用asynchronus请求试试?

I have been testing this in my app and I haven't find any trouble. But I'm afraid of this synchronus behaviour because of old experiences with jQuery and AJAX. My app gets a big amount of data from several tables in a database, and this requires a time. For each bunch of data retrieved from a table, I need to instantly process it to not delay the whole thing too much. Meanwhile, the user is interacting with the browser, so it can be blocked, and I thought that web workers would work fine. Do you thing that this is a good solution? Or should I try with asynchronus requests?

感谢你。

推荐答案

我没有确凿的事实,但既然你问的意见... :)

I don't have hard facts, but since you asked for opinions... :)

目前在Chrome中讲述的问题:太多的Web Workers可能会导致无声崩溃(上限〜60-100,根据的这个错误报告)。普遍的问题是网络的工人是资源密集型的,至少在V8引擎。

There is a telling issue in Chrome: Too many Web Workers can cause a silent crash (caps ~60-100, according to this bug report). The general problem is Web Workers are resource intensive, at least with v8.

假设你将最终使多个HTTP调用,如果你做同步HTTP调用的Web工作:

Assuming you're going to end up making multiple HTTP calls, if you're doing synchronous HTTP calls in a Web Worker:

  • 在某种意义上,你的交易的异步Web工人异步HTTP调用,这只会增加一个中间混进去,你仍然需要管理的事情异步。
  • 如果你去了更简单,更有效利用资源的路线,只使用一个网络工作者,你会花大量的时间等待它给你答复。
  • 如果,在另一方面,你使用多个网络工作者,你可能会需要跟踪其中一个是免费的,这一个是忙,等等,在这种情况下,您将创建一个土生土长的调度,而不是用什么来烤到浏览器中。
  • 最后,网络工作者是昂贵的(显然),你可能最终只是创建多个Web工人,使他们可以坐在那里等待一个HTTP调用来完成。

我不认为自己对此事的专家,所以请把这个东西它的价值。

I do not consider myself an expert on the matter, so please take this for what it's worth.

更新:增加了一些优点/缺点了各种方案

Update: Adding some pros / cons for various scenarios.

有些优点/缺点使得同步和异步HTTP调用之间进行选择时浮现在脑海中使用Web工作时:

Some pros / cons that come to mind when choosing between making synchronous and asynchronous HTTP calls when using a Web Worker:

  • 在通常情况下,同步请求将是更容易编写,并会导致code,很容易效仿。同步请求的一个缺点是,他们可能会鼓励编写应雕刻成单独的,更小的函数长功能。
  • 如果你正在做一个单呼,在它里面需要两种方法和同步是更好,因为它是一个简单一点之间完成的时间没有区别。我说这只是一个简单一点,因为有一个回调监听一个异步调用实在是相当简单的。
  • 如果你正在做的事会发生在一个特定的顺序,如加载用户的配置文件数据,然后让基于他们的地址,同步调用会更好,因为它会更容易编写和当地的天气多次调用更容易阅读。关于阅读它更主要的是在调用顺序依赖将被清楚地说明通过同步调用及其功能顺序的选择。在多打几个电话也有,更多的这没关系。如果有很多呼叫,在复杂性的差异很可能是急剧。
  • 如果你不得不做出这样不需要发生在任何特定的顺序多次调用,然后异步请求是更好,因为整个过程很可能是数量级比同步请求更快。你正在做的更多的呼叫或更慢的连接,在总时间越显著差异会;这种差异会非常快速地增长(指数?)。从某人读取code的角度来看,我认为使用同步请求,在这种情况下,将是有所的误导,因为这将意味着有顺序性质的呼叫,即使没有。从写一系列异步请求不依赖于对方的角度来看,它应该不会太糟糕,因为你只设置一个计数器,使所有的电话,增加计数器在每个回调,你就完蛋了当计数器等于你的呼叫的数量。
  • Generally, synchronous requests will be easier to write and will result in code that is easy to follow. A downside of synchronous requests is they might encourage writing long functions that should be carved out into separate, smaller functions.
  • If you're making a single call, there is no difference in the time it takes to finish between the two methods and synchronous is better because it's a bit simpler. I say it's only a bit simpler because a single asynch call with one callback listener is really rather simple.
  • If you're making multiple calls that have to happen in a specific sequence, like loading a user's profile data and then getting the local weather based on their address, synchronous calls will be better because it will be easier to write and a lot easier to read. The main thing about reading it is the sequential dependencies in the calls will be clearly outlined by the choice of making the calls synchronously and their order in the function. The more calls there are, the more this will matter. If there are many calls, the difference in complexity is likely to be drastic.
  • If you have to make multiple calls that do not need to happen in any specific order, then asynchronous requests are better because the overall process is likely to be orders of magnitude faster than with synchronous requests. The more calls you're making or the slower the connection, the more significant the difference in total elapsed time will be; this difference will grow very quickly (exponentially?). From the perspective of someone reading the code, I think using synchronous requests, in this situation, would be somewhat misleading as it would suggest there is a sequential nature to the calls even though there is not. From the perspective of writing a series of asynch requests that are not dependent on each other, it shouldn't be too bad because you just setup a counter, make all the calls, increment the counter in each of the callbacks and you're finished when the counter equals the number of calls you made.

这篇关于意见关于网络工作者同步请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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