在 JavaScript Web Worker 中休眠而不牺牲 CPU 资源

发布于 2024-11-16 16:22:37 字数 939 浏览 3 评论 0原文

概述: 我们公司需要能够访问本地资源(RS232/串行设备)的 Web 应用程序。经过一些研究和 POC 之后,我们放弃了 ActiveX、Java Applet 等选项,并决定创建本地应用程序(用 C# 编写,稍后将转换为服务并分发给客户),该应用程序读取串行数据并通过 HTTP 协议为其提供服务(简单的 TCP 服务器应答必要的标头 + 普通串行数据)。然后,Web 应用程序通过 AJAX 来“http://localhost:8080”读取这些数据。

所有这些都是在 Web Worker 中通过 while 循环将消息发布到主线程并将其填充到表单的输入元素中完成的。通过提供静态内容,我可以获得不错的性能。在 Windows 上 Chrome 13 中较旧的 Intel 双核(不是 Core 2 Duo)1.6GHz CPU 上,每秒有 300-350 次迭代,Web 工作线程占用 5 - 9% 的 CPU。

问:我现在想要实现的是通过在每个 ajax 请求后插入某种 sleep() 函数来限制 Web Worker 中的轮询间隔,例如在开始时尝试 100 毫秒。

在不牺牲线程中 CPU 资源的情况下最好的解决方案是什么?

注意:作为最后的手段,我可​​以在 TCP 服务器代码中插入一些延迟。

编辑:

我需要睡在工人里面。示例(为了清晰起见,过于简单化):

AJAX = new XMLHttpRequest();
while (true) {
  AJAX.open("GET", "http://127.0.0.1:8080", false);
  AJAX.send(null);
  var ean = AJAX.responseText;
  if (ean != '') { postMessage(ean); }
  /* NEED TO SLEEP HERE WHETHER THE RESPONSE WAS SENT OR NOT */
}

Overview:
Our company has a need for web application that has access to local resources (RS232 / serial devices). After some research and POC we've discarded options like ActiveX, Java Applets and decided to create local application (written in C# which will be later transofmed to a service and distributed to the customers) that reads serial data and serves them over HTTP protocol (simple TCP server answering necessary headers + plain serial data). Then the web application does AJAX to 'http://localhost:8080' reading those data.

All this is done in a web worker in a while loop posting message to the main thread filling it in a form's input element. By serving static content I am able to get decent performance. On an older Intel Dual Core (not Core 2 Duo) 1,6GHz CPU in Chrome 13 on Windows there are 300-350 iterations per second and 5 - 9% CPU taken by the web worker thread.

Q: What I want to achieve now is to throttle polling interval in the web worker by inserting some sort of sleep() function after each ajax request, e.g. experiment with 100ms in the beginning.

What would be the best solution without sacrificing CPU resources in the thread?

Note: I could insert some delay in the TCP server code as the last resort.

EDIT:

I need to sleep inside worker. Example (oversimplified for clarity):

AJAX = new XMLHttpRequest();
while (true) {
  AJAX.open("GET", "http://127.0.0.1:8080", false);
  AJAX.send(null);
  var ean = AJAX.responseText;
  if (ean != '') { postMessage(ean); }
  /* NEED TO SLEEP HERE WHETHER THE RESPONSE WAS SENT OR NOT */
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2024-11-23 16:22:37

我认为最好的解决方案是 WebSocket API。因此,您可以在 Web 应用程序中附加一个事件侦听器,并在每次需要执行某些操作时从 C# 触发它。

I think the best solution would be the WebSocket API. So, you can attach a event listener in your web application and fire it from C# every time you need some action.

断念 2024-11-23 16:22:37

不幸的是,JavaScript 中没有睡眠或等待功能 - 但您可以使用超时和回调来模拟一个功能。 (通过回调函数“跳转”到代码的其余部分 - 并将调用置于 setTimeout 方法中)。

编辑:

您可以执行类似的操作,

AJAX = new XMLHttpRequest();
var myInterval = setInterval( function(){
    AJAX.open("GET", "http://127.0.0.1:8080", false);
    AJAX.send(null);
    var ean = AJAX.responseText;
    if (ean != '') { postMessage(ean); }
},120);

现在,如果您不想等待第一个调用,请将其放置在间隔之外(因此它会立即运行),然后再次调用间隔,如上面所写。

Unfortunately there is no sleep, or wait, function in javascript - but you can emulate one with the use timeouts and callbacks. ("Jumping" to the rest of your code through a callback function - and placing the call in a setTimeout method).

EDIT:

You can do something like this,

AJAX = new XMLHttpRequest();
var myInterval = setInterval( function(){
    AJAX.open("GET", "http://127.0.0.1:8080", false);
    AJAX.send(null);
    var ean = AJAX.responseText;
    if (ean != '') { postMessage(ean); }
},120);

Now if you don't awnt to wait for the first call, place it outside of the interval (so it is ran instantly) and then call the interval again as it is written above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文