终止之后iOS Javascript Workers高CPU() [英] iOS Javascript Workers High CPU after terminate()

查看:100
本文介绍了终止之后iOS Javascript Workers高CPU()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的javascript函数,可能需要1秒或多分钟发送答案。所以我创建了一个正在工作的Worker,我在Swift的UIWebView中调用了这个函数(stringByEvaluatingJavaScriptFromString)。我只等了5秒钟(在同一个Javascript中创建了超时),之后我终止了worker(job.terminate()),然后我用另一个参数(简化)启动了另一个参数(在show中只用了1秒)答案。问题是,即使在终止信号之后,第一个工作人员似乎也在后台运行。正如您在图像中看到的那样,WebCore:Worker具有大量的CPU使用率。我怎样才能相继终止工人?

I have a complex javascript function which could take 1 second, or many minutes sending an answer. So I created a Worker which is working, I'm calling this function from my UIWebView in Swift (stringByEvaluatingJavaScriptFromString). I'm waiting only 5 seconds (timeout created in the same Javascript), after that I terminate the worker (job.terminate()), and I start a different one with other parameters (simplier), which takes only 1 second in show the answer. The thing is, the first worker seems to be running in background even after the terminate signal. AS you can see in the image, the WebCore: Worker has a lot of CPU usage. How can I relly terminate the worker?

job = new Worker("main.js");

var t = setTimeout(function(){
  stop();
  logNode.innerText = 'NULL';
},5000);

function stop() {
    job.terminate();
    job = undefined;
}

(main.js):

importScripts('dist/algorithm.js');

var lp;

self.addEventListener('message', function(e) {
...
...


推荐答案

终止worker(并确保它)的最简单方法是注入一个布尔表达式以使代码其主循环的异常输出(如果代码中只有1或2个瓶颈,甚至可以优雅地返回。)。并且您可以在让它自然退出之前在catch块中执行任何所需的清理。

the easiest way to terminate the worker (and be sure of it) would be to inject a boolean expression to cause the code to "exception out" of its main loop (or even return gracefully if there is only 1 or 2 bottlenecks in the code.). and you can perform any required cleanup in a catch block prior to letting it exit naturally.

我个人讨厌使用终止或终止等函数结束线程,因为它可以使事物处于未定义状态

I personally hate ending threads with functions such as terminate or kill because it can leave things in a undefined state

终止的调用因为工作人员正在使用它可以留下没有时间进行消息处理的所有资源,因此可能会被忽略或排队等等。

The call to terminate may get ignored or enqueued for later simply because the worker is using all the resources it can leaving no time for message processing.

这篇关于终止之后iOS Javascript Workers高CPU()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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