是否可以在外部暂停/恢复网络工作者? [英] Is it possible to pause/resume a web worker externally?

查看:30
本文介绍了是否可以在外部暂停/恢复网络工作者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到 web worker 有一个 terminate() 函数,但是有没有办法像中断一样从宿主线程暂停和恢复 web worker?

I've seen that web workers have a terminate() function, but is there a way to pause and resume web workers from the host thread similar to interrupts?

这个问题的关键因素是在 web worker 上使用 terminate() 调用会破坏 worker 内部的状态(并且发生的任何操作都可能在半完成状态下被破坏.)我'我正在寻找一种机制,可以在数十万个任务之间进行切换,无论多少 Web Worker 与机器上的总内核数相等,而不会丢失这些任务中的状态或依赖于像 Fiber 这样的协作多线程风格.

The critical factor with this question is that using the terminate() call on a web worker will break the state internal to the worker (and any operations taking place may in turn be corrupted in a half-complete state.) I'm looking for a mechanism by which to switch between hundreds-thousands of tasks held in however many web workers equal the total cores on the machine, without losing state within those tasks or relying on cooperative multithreading styles like fibers.

推荐答案

是否可以在外部暂停/恢复网络工作者?

Is it possible to pause/resume a web worker externally?

没有.没有 webWorker 本身的一些合作,浏览器 javascript 没有办法从外部暂停 web Worker.

No. Browser javascript does not have a means of externally pausing web workers without some cooperation from the webWorker itself.

变通方法(与 webWorker 合作)将涉及 webWorker 运行一些代码,然后允许来自外部世界的消息到达并告诉它暂停,然后 webWorker 本身不调用下一个单元工作,直到它收到恢复处理的消息.但是,因为即使是 webWorkers 也是事件驱动的,所以在你完成你正在做的事情并返回到事件系统之前,你无法接收来自外部世界的消息.

A work-around (with cooperation from the webWorker) would involve the webWorker running a bit of code, then allowing a message to arrive from the outside world that tells it to pause and then the webWorker itself not calling the next unit of work until it receives a message to resume processing. But, because even webWorkers are event driven, you can't receive a message from the outside world until you finish what you were doing and return back to the event system.

这可能涉及执行一大块工作,从现在开始只设置一个计时器,允许消息到达几毫秒,然后当消息到达时,您存储新的暂停/恢复状态,然后当计时器触发时,你检查状态,如果它暂停,你就不会调用下一个工作单元.如果它不是暂停状态,则执行下一个工作单元,当它完成时,再次在很短的时间内执行 setTimeout()(允许处理任何挂起的消息)并继续执行再次.

This would probably involve executing a chunk of work, setting a timer for only a few ms from now which allows messages to arrive, then when a message arrives, you store the new pause/resume state and then when the timer fires, you check the state and if its pause, you don't call the next unit of work. If it's not the pause state, you execute the next unit of work and when it finishes, you again do setTimeout() with a really short time (allowing any pending messages to process) and keep going over and over again.

不幸的是,这种技术要求您将工作分块,这首先否定了 webWorkers 的一些好处(尽管您仍然保留使用多个 CPU 处理工作的好处).

Unfortunately, this technique requires you to chunk your work which negates some of the benefits of webWorkers in the first place (though you still retain the benefit of using more than one CPU to process work).

我正在寻找一种机制,通过该机制可以在数十万个任务之间进行切换,而网络工作者的数量等于机器上的总内核数,而不会丢失这些任务中的状态

I'm looking for a mechanism by which to switch between hundreds-thousands of tasks held in however many web workers equal the total cores on the machine, without losing state within those tasks

您需要让每个当前正在运行的 webWorker 合作并允许处理来自外部世界的消息,以便他们可以看到外部世界何时希望他们停止做更多的工作,然后让他们合作而不是调用下一个单元工作,这样他们就不会做任何事情,直到下一条消息告诉他们重新开始工作.

You would need to have each currently running webWorker cooperate and allow a message from the outside world to get processed so they could see when the outside world wants them to stop doing more work and then have them cooperatively not call the next unit of work so they wouldn't be doing anything until the next message arrives telling them to start doing their work again.

如果您要拥有数以千计的这些,您可能不希望它们都成为单独的 webWorker,因为这对系统来说有点沉重.您可能需要一组在工作队列上工作的 webWorker.您将要处理的工作单元排队,每个 webWorker 从队列中获取下一个工作单元,对其进行处理,然后获取下一个单元,依此类推.然后,您的主进程只会让队列接受您希望 webWorker 执行的任何工作.这还需要将工作分成适当的工作单元.

If you're going to have thousands of these, you probably don't want them all to be individual webWorkers anyway as that's a bit heavy weight of a system. You probably want a set of webWorkers that are working on a work queue. You queue up work units to be processed and each webWorker grabs the next unit of work from the queue, processes it, grabs the next unit and so on. Your main process then just keeps the queue fed with whatever work you want the webWorkers to be doing. This also requires chunking the work into proper units of work.

这篇关于是否可以在外部暂停/恢复网络工作者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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