Javascript 或 jQuery 中的线程安全队列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4711423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread-safe queue in Javascript or jQuery
提问by at.
I have many asynchronous AJAX calls whose results will get processed. It doesn't matter what order the processing occurs, but the results need to get processed one at a time. So I'd like to simple do my AJAX calls and they all just put their results in a single queue. That queue should then get processed on a single thread. This way the results get processed one at a time as soon as possible.
我有许多异步 AJAX 调用,它们的结果将得到处理。处理发生的顺序无关紧要,但结果需要一次处理一个。所以我想简单地做我的 AJAX 调用,他们都只是把他们的结果放在一个队列中。然后应该在单个线程上处理该队列。这样一来,结果会尽快得到处理。
What's the best way to do this? I'm using jQuery, so happy to take advantage of any facilities it provides for this.
做到这一点的最佳方法是什么?我正在使用 jQuery,很高兴利用它为此提供的任何工具。
回答by Josh Smeaton
Asynchronous does NOT mean 'multiple threads'. Think about many click events firing in a row, before the first click handler is processed. Only one action can be handled at a time, and the others will wait to execute.
异步并不意味着“多线程”。考虑在处理第一个单击处理程序之前连续触发的许多单击事件。一次只能处理一个动作,其他动作将等待执行。
Event driven languages like Javascript operate on the basis of a queue. Javascript in the background essentially has one giant queue that events and asynchronous responses get pushed in to. Once a particular piece of processing is complete, the next item from the queue is worked on.
Javascript 等事件驱动语言基于队列运行。后台的 Javascript 本质上有一个巨大的队列,事件和异步响应被推送到其中。一旦某个特定的处理完成,队列中的下一个项目就会被处理。
These queues are sometimes known as 'Runloops'. Javascript will spin in an endless loop, retrieving an event from the queue, processing it, and returning back to the queue for another piece of work.
这些队列有时被称为“Runloops”。Javascript 将无限循环,从队列中检索事件,处理它,然后返回队列进行另一项工作。
Multithreading can be achieved in (newer) versions of Javascript using Web Workers, but these are explicitly opt-in. Look them up if you're interested.
可以使用Web Workers在(较新的)Javascript 版本中实现多线程,但这些是明确选择的。有兴趣的可以去看看。
To answer your question then, just attach a callback to your asynchronous request, and it will complete processing even if another response is returned half way through. The other response will 'wait' until the current event is handled.
要回答您的问题,只需将回调附加到您的异步请求,即使中途返回另一个响应,它也会完成处理。另一个响应将“等待”直到处理当前事件。
回答by Kyle Wild
If your only concern is the browser (since you mention jQuery, I assume that's true), you should know that Javascript is single-threaded in browsers.
如果您唯一关心的是浏览器(因为您提到了 jQuery,我认为这是真的),您应该知道Javascript 在浏览器中是单线程的。
回答by beeglebug
The simplest way would be to implement your own queue system for the callbacks (using push/pop to emulate a stack).
最简单的方法是为回调实现自己的队列系统(使用 push/pop 模拟堆栈)。
As each asynchronous request returns, add the data to the end of an array and fire off some kind of queue.process()command, which would handle the first element in the array.
当每个异步请求返回时,将数据添加到数组的末尾并触发某种queue.process()命令,该命令将处理数组中的第一个元素。
Finally, add something like if(alreadyRunning) { return; }to the top of that function to stop it doing more than one at once, and you've got your single threaded queue.
最后,if(alreadyRunning) { return; }在该函数的顶部添加类似的内容以阻止它一次执行多个任务,这样您就拥有了单线程队列。
edit:removed code, some people were getting hung up on it instead of the original question
编辑:删除了代码,有些人被挂断了而不是原来的问题
回答by Gutzofter
You might want to take a look at Event messaging between JavaScript objects.
您可能想查看JavaScript 对象之间的事件消息传递。
I used an implementation similar to the blog post with a jQuery Plugin called ajaxMonitor.
我使用了一个类似于博客文章的实现,其中包含一个名为ajaxMonitor的 jQuery 插件。
The way it works as soon as any of the callbacks from the AJAX request get called it updates a HTML table. The callbacks happen asynchronous.
只要来自 AJAX 请求的任何回调被调用,它的工作方式就会更新 HTML 表。回调发生异步。
The very nature of AJAX means asynchronous. So processing your QUEUE in the success callback of your AJAX request, I believe would accomplish what your looking for.
AJAX 的本质意味着异步。因此,在您的 AJAX 请求的成功回调中处理您的 QUEUE,我相信会完成您所寻找的。

