简体   繁体   English

Web Worker类收集垃圾时会发生什么

[英]What happens when a web worker class gets garbage collected

I have a class which handles the creation of a new web worker when the class is instantiated, post a message to the worker that returns a promise that gets resolved when the worker is done with the task. 我有一个类,该类在实例化该类时处理新Web工作者的创建,向该工作者发布消息,该消息返回一个诺言,当该工作者完成任务时,该诺言将得到解决。 Basically, I am calling a class which handles creating, posting, listening and terminating the worker. 基本上,我正在调用一个处理创建,发布,侦听和终止工作人员的类。

However, if I create a new instance of this class, post a message, and end my function, what happens to the worker? 但是,如果我创建此类的新实例,发布一条消息,然后结束我的功能,那么工作人员会怎样? Does it become orphaned and keeps running its task until it's done? 它会变成孤立的并继续运行其任务直到完成吗?

Also, this is a bit tangential, but how bad would it be to instantiate n number of workers at once? 另外,这有点切线,但是一次实例化n个工人将有多糟糕? 8? 8? 20? 20? 50? 50? How does the browser handle that? 浏览器如何处理?

Does it become orphaned and keeps running its task until it's done? 它会变成孤立的并继续运行其任务直到完成吗?

Yes. 是。 Per the spec, in this instance the event loop must run to completion, the worker gets marked as "suspendable" and finally, the 根据规范,在这种情况下,事件循环必须运行到完成,该工作程序被标记为“可暂停”,最后,

Reference Step 10 参考步骤10

how bad would it be to instantiate n number of workers at once? 一次实例化n个工人有多糟糕? 8? 8? 20? 20? 50? 50?

You can play here: http://math.hws.edu/eck/jsdemo/jsMandelbrot.html 您可以在这里玩: http : //math.hws.edu/eck/jsdemo/jsMandelbrot.html

I have personally seen 16 worker apps humming along. 我个人已经看到16个工作者应用程序嗡嗡作响。 There is a trade off with spinup/teardown and messaging that must be considered. 必须考虑到旋转/拆卸和消息传递之间的权衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM