简体   繁体   English

终止WebWorker(JavaScript)

[英]Terminate a WebWorker (Javascript)

I am writing a language for educational purpose, which should run in web browsers. 我正在编写一种用于教育目的的语言,该语言应在网络浏览器中运行。 The language compiles to JavaScript. 该语言可编译为JavaScript。

So there is the problem of endless loops and recursion. 因此存在无限循环和递归的问题。 Until that moment I thought the solution would be to have the runtime implemented as a WebWorker and a button on the ui, to terminate it if it runs to long. 在那之前,我认为解决方案是将运行时实现为WebWorker,并在ui上添加一个按钮,如果运行时间过长,则将其终止。

I just found out, while testing my language, that the web worker isn't really terminated, by calling : runtime.terminate() ; 我只是在测试我的语言时发现,通过调用runtime.terminate() ,Web Worker并没有真正终止。

At least not until his last job has finished, i haven't tested what happens if it would be finished. 至少直到他的上一份工作完成为止,我还没有测试如果完成该怎么办。 Even in the documentation I found so far, its stated that it stops execution immediately. 即使在到目前为止找到的文档中,它也指出它会立即停止执行。

So even if the user can continue working that way, the running worker in the background can use up resources and lead to at least a not responding browser. 因此,即使用户可以继续那样工作,后台正在运行的工作程序也可能耗尽资源并导致浏览器至少没有响应。

So I need any way to terminate the execution in case of an endless loop or recursion, in response to a user event. 因此,为了响应用户事件,在发生无限循环或递归的情况下,我需要任何方法来终止执行。 As I din't find any way to put a thread to sleep in JavaScript, this doesn't work either, or is there something like sleep for web workers? 由于我找不到任何使线程在JavaScript中进入睡眠状态的方法,因此该方法也不起作用,或者Web工作人员是否有诸如睡眠之类的东西?

Have you got any suggestions what I could do? 您有什么建议我可以做什么?

I would do it like this: 我会这样做:

1) Have your compiler generate code such that instead of executing the program from start to finish, it breaks it up into segments - for example, the body of a loop or the body of a function could be a segment. 1)让编译器生成代码,而不是从头到尾执行该程序,而是将其分成多个段-例如,循环的主体或函数的主体可以是一个段。

2) Each segment should end with a timeoutId = setTimeout(nextSegment, 0) . 2)每个段应以timeoutId = setTimeout(nextSegment, 0) This allows the web worker to receive and respond to asynchronous events in between executing segments. 这允许Web Worker在执行段之间接收并响应异步事件。

3) When you want to terminate the web worker, use window.postMessage() to send it an asynchronous message. 3)当您要终止Web Worker时,请使用window.postMessage()向其发送异步消息。

4) Inside the web worker, have an event handler that, upon receiving that message, does a clearTimeout(timeoutId) . 4)在网络工作者中,有一个事件处理程序,该事件处理程序在收到该消息后会执行clearTimeout(timeoutId)

Thanks for the response, I have thought about this way, so actually I wouldn't need the web worker for it. 感谢您的答复,我已经考虑过这种方式,因此实际上我不需要网络工作者。 So the downside of this approach is: 因此,这种方法的缺点是:

1) That I would have to implement the loops as recursive function calls ... 1)我将必须将循环实现为递归函数调用...

And the bigger problem 2) The segment size has to be limited to one command, that this approach actually works, so I would have to wrap each command into a function that calls the next one, which would be a huge overhead... 更大的问题2)段的大小必须限制为一个命令,这种方法才能真正起作用,因此我将必须将每个命令包装到一个调用下一​​个命令的函数中,这将是巨大的开销...

Thats why I am looking for a way around, if there is any? 那就是为什么我要寻找解决方法(如果有)?

Yes I'm writing a compiler. 是的,我正在编写一个编译器。

As working on this approach I must mention, it might be that the overhead for the user might not be much, but the impact on compilation is really huge... It's like building a VM on top of Javascript... 在使用这种方法时,我必须提到,可能用户的开销可能不大,但是对编译的影响确实很大……这就像在Javascript之上构建VM一样……

What I got to this point is that additional to the above mentioned points I need also to: 我要说的是,除了上述要点之外,我还需要:

-need an complex indexing system for function naming -take care of variable allocation -have to solve expressions in series -... -需要用于函数命名的复杂索引系统-注意变量分配-必须解决串联表达式-...

That's why I am looking for a more elegant variant... 这就是为什么我在寻找更优雅的版本...

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

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