我想知道您对此有何看法.是否建议在 web worker 中使用同步请求(XMLHttpRequest)?我能找到什么问题?
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 的旧经验,我害怕这种同步行为.我的应用程序从数据库中的多个表中获取大量数据,这需要时间.对于从表中检索到的每一束数据,我需要立即处理它,以免过多地延迟整个事情.同时,用户正在与浏览器交互,所以它可以被阻止,我认为网络工作者可以正常工作.你认为这是一个好的解决方案吗?还是我应该尝试异步请求?
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 Worker 过多会导致无声崩溃(根据 这个错误报告).一般的问题是 Web Workers 是资源密集型的,至少在 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 调用,如果您在 Web Worker 中进行同步 HTTP 调用:
Assuming you're going to end up making multiple HTTP calls, if you're doing synchronous HTTP calls in a Web Worker:
我不认为自己是这方面的专家,所以请接受它的价值.
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.
在使用 Web Worker 进行同步和异步 HTTP 调用之间进行选择时想到的一些优点/缺点:
Some pros / cons that come to mind when choosing between making synchronous and asynchronous HTTP calls when using a Web Worker:
更新:@rbrundritt 制作了一个非常有趣且相关的 对此答案的评论中的观察:
Update: @rbrundritt makes a very interesting and relevant observation in a comment on this answer:
我发现与网络工作者一起工作的一件事是,他们似乎每个人都获得了自己的 http 限制.浏览器将并发 http 请求的数量限制在 8 或 12 左右,具体取决于限制之前的浏览器,如果您有大量请求要处理,这可能是一个瓶颈.我发现如果我通过我的请求网络工作者,每个人可以在开始节流之前执行 8 到 12 个并发请求.这对某些应用来说可能是一个巨大的好处.
One thing I found working with web workers is that they appear to each gain their own http limit. Browsers limit the number of concurrent http requests to around 8 or 12 depending on the browser before throttling, which can be a bottle neck if you have a lot of requests to process. I've found that if I pass my requests web workers, each can do 8 to 12 concurrent requests before they start throttling. This can be a huge benefit for some apps.
@rbrundritt
这篇关于关于 Web Worker 中的同步请求的意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
NETWORK_ERROR:XMLHttpRequest 异常 101NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 异常 101)
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)