如果关闭创建此Web Worker的页面,Web Worker会发生什么情况? [英] What happens to a Web Worker if I close the page that created this Web Worker?

查看:689
本文介绍了如果关闭创建此Web Worker的页面,Web Worker会发生什么情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为Main.html的页面,它创建了一个web worker。如果我通过更改window.location来关闭主页面,Web工作人员会被终止还是Web工作人员仍在运行?



Firefox或Chrome如何选择在页面关闭后处理长时间运行的Worker任务?如果工作人员的任务是发送非常快速的POST请求,对于这种情况,浏览器是否在页面关闭后立即终止工作人员,或浏览器是否允许工作人员完成POST请求?

解决方案

关闭或导航离开页面会导致浏览器丢弃该页面的文档

lockquote

用户代理可能会丢弃顶部(通常是响应用户的请求,例如当用户关闭一个包含一个或多个顶级浏览上下文的窗口时)。

W3C工作人员规范表示工作人员有一个文档的。对于具有窗口(即不是工作人员)的网页产生的工作人员,该列表由单个 Document 产生它的页面。规范继续说:


每当 Document 对象被丢弃时,必须从列表中包含该文档的每个工作人员的 Documents 列表中删除。


此外:


如果工作人员列表中的工作人员被称为允许工作人员工作人员的 Document s不是空的。


这个定义稍后使用:


关闭孤儿工人:开始监控工作人员,使其......不迟于允许工作人员,工作人员全局范围的结束标志设置为true。

最后,阻止worker的新活动:


一旦WorkerGlobalScope的关闭标志设置为true,事件循环的任务队列必须是光盘以及将会添加到它们的任何进一步的任务(已经在队列中的任务不受影响,除非另有指定)。实际上,一旦结束标志为真,定时器停止触发,所有挂起异步操作的通知都会被丢弃等。

仍然运行的长时间运行可能会被浏览器暂停,但不一定是:


用户代理可以调用​​kill a工人随时处理模型,例如响应用户请求,响应CPU配额管理,或者当工人停止成为活动的需要工作人员时,如果工人继续执行,即使其关闭标志设置为true,也是如此。因此,W3C规范允许工作者继续运行,但不允许它处理任何进一步的事件(工作者消息,定时器,网络通信回调等)。由于性能原因,特定的浏览器可能会随时选择杀死该工作人员。在页面关闭后,每个浏览器选择处理长时间运行的Worker任务的方式超出了规范的范围;它是实现定义的。你不应该依赖这种行为:你应该容忍立即终止,也应该容忍任何长时间运行的任务 not 立即终止关闭页面。



共享工作人员遵守完全相同的规则,除了他们的 Document s列表包含所有 Documents 目前正在使用它们,所以它们的结束标志只有在所有 Documents 已关闭后才设置。


Let's say I have a page called Main.html that creates a web worker. If I close the Main page by changing window.location, would the web worker be terminated or would the web worker be still running?

How does Firefox or Chrome choose to "handle long-running Worker tasks after the page has closed"? If the worker's task is to send a very quick POST request, for this case, does the browser terminate the worker immediatly after the page is closed or does the browser allow the worker to finished its POST request?

解决方案

Closing or navigating away from a page causes the browser to discard the page's document:

User agents may discard top-level browsing contexts at any time (typically, in response to user requests, e.g. when a user closes a window containing one or more top-level browsing contexts).

The W3C Worker spec says that a Worker has a "list of Documents". For workers spawned by a web page with a window (i.e., not a Worker), the list consists of the single Document of the page that spawned it. The spec goes on to say:

Whenever a Document object is discarded, it must be removed from the list of the worker's Documents of each worker whose list contains that Document.

And also that:

A worker is said to be a permissible worker if its list of the worker's Documents is not empty.

This definition is used later:

Closing orphan workers: Start monitoring the worker such that... no later than it stops being a permissible worker, worker global scope's closing flag is set to true.

Finally, the "closing flag" prevents new activity on the worker:

Once the WorkerGlobalScope's closing flag is set to true, the event loop's task queues must discard any further tasks that would be added to them (tasks already on the queue are unaffected except where otherwise specified). Effectively, once the closing flag is true, timers stop firing, notifications for all pending asynchronous operations are dropped, etc.

Any old, long-running activity still running may be halted by the browser, but does not have to be:

User agents may invoke the "kill a worker" processing model on a worker at any time, e.g. in response to user requests, in response to CPU quota management, or when a worker stops being an active needed worker if the worker continues executing even after its closing flag was set to true.

Thus, the W3C specification allows for the worker to continue running, but does not allow it to handle any further events (worker messages, timers, network communication callbacks, etc.). A particular browser may choose to kill the worker at any time for performance reasons. The way that each browser chooses to handle long-running Worker tasks after the page has closed is outside the scope of the specification; it is implementation-defined. You should not depend on this behavior one way or the other: you should tolerate immediate termination, and should also tolerate any long-running tasks not immediately terminating with the closure of the page.

Shared Workers are subject to the exact same rules, except that their list of Documents includes all Documents currently using them, so their closing flag is set only after all the Documents have been closed.

这篇关于如果关闭创建此Web Worker的页面,Web Worker会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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