Web workers能够由浏览器在任何时候重新启动吗?

huangapple go评论77阅读模式
英文:

Can web workers be restarted by the brower at any time?

问题

我在许多地方都读到(包括这里,在Stack Overflow),Web Worker可以随时被浏览器杀死并重新启动。好吧,也许“随时”意味着“只要它们在做某事”,但问题是它们可以在没有事先警告的情况下被浏览器杀死并重新启动,丢失在globalThis中存储的任何数据。

但无论如何,我找不到规范中的相关信息,这让我担心,因为我实际上正在使用一个依赖于在全局变量中保存一些信息数据以在消息处理函数的调用之间保持最小状态的Web Worker。

这个Web Worker运行得非常顺利,状态也保持得很好,除非页面被刷新,但我担心如果浏览器决定重新启动Web Worker,那么应用程序可能会失败,肯定会失败。

我已经在Google上搜索了这个问题,特别是寻找示例和替代方法,以重写我的Web Worker,而不需要全局状态,但我没有找到任何相关的信息。

有人可以为我指出一些官方信息吗?

英文:

I've read in many places (including here, in Stack Overflow) that web workers can be killed and restarted by the browser at any time. Well, probably "at any time" means "as long as they are doing something", but the thing is that they can be killed and restarted by the browser without prior warning, losing any data stored in globalThis.

But for the life of me I cannot find that in the specification, and it worries me because I'm actually using a Web Worker whose proper functioning relies in keeping some info data in a global variable to keep minimal state between calls of the message handling function.

This web worker works like a charm, and state is kept unless of course the page is refreshed, but I'm afraid that the app can fail if the browser decides to restart the web worker, and for sure it will if that happens.

I've googled about this, specially looking for examples and alternatives for rewriting my web worker without the need for that global state, but I haven't found anything relevant.

Can anyone point me to some official information about this?

答案1

得分: 4

我在许多地方都看到(包括在这里,Stack Overflow中)Web Workers可以在任何时候被浏览器终止并重新启动。

不会的,浏览器不会以任何方式重新启动你的(专用)Worker。

浏览器可以在以下情况下终止Worker:

  • 主可导航的页面被终止。
  • 调用了Worker#terminate()方法。
  • 调用了DedicatedWorkerGlobalScope#close()方法,并且当前任务已完成。
  • Worker变成了"孤儿"。这可能发生在以下情况下:
    • 当Worker是子Worker(由Worker本身创建)且其所有者已被终止,
    • 或者当它没有计划的任务,没有正在进行的网络请求或数据库事务,以及没有可以接收来自外部的消息的MessagePort或内部Worker对象时。

只要你在主线程中保留对Worker对象的引用,不调用关闭方法之一,浏览器就不会终止Worker。


注意:对于SharedWorkers、ServiceWorkers和其他Worklets的规则都不同,本答案仅处理通过new Worker()创建的专用Workers。

英文:

> I've read in many places (including here, in Stack Overflow) that web workers can be killed and restarted by the browser at any time.

Nope, they won't ever restart your (dedicated) Worker in any way.

The browser can kill a Worker if

  • The main navigable (a.k.a "page") is killed.
  • The Worker#terminate() method has been called.
  • The DedicatedWorkerGlobalScope#close() method has been called, and the current task is completed.
  • The Worker becomes "orphan". This may happen when it stops being a "protected worker", i.e.
    • when the Worker is a sub-Worker (itself created from a Worker) and its owner has been killed,
    • or when it has no scheduled tasks, no ongoing network request or database transaction, and no MessagePort or inner Worker objects susceptible of receiving messages from the outside.

So as long as you keep a reference to your Worker object in your main thread, and don't call yourself one of the closing methods, there is no way the Worker can be killed by the browser.


Note: The rules for SharedWorkers, ServiceWorkers, and other Worklets are all different, this answer treats only Dedicated Workers, created from new Worker().

huangapple
  • 本文由 发表于 2023年2月9日 03:25:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定