Javascript或jQuery中的线程安全队列

时间:2022-08-11 11:47:43

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,很高兴利用它为此提供的任何设施。

4 个解决方案

#1


25  

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.

要回答你的问题,只需将回调附加到异步请求,即使在中途返回另一个响应,它也会完成处理。另一个响应将“等待”,直到处理当前事件。

#2


5  

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在浏览器中是单线程的。

#3


0  

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

编辑:删除代码,有些人被挂起来而不是原来的问题

#4


0  

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请求时处理你的队列,我相信会完成你想要的。

#1


25  

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.

要回答你的问题,只需将回调附加到异步请求,即使在中途返回另一个响应,它也会完成处理。另一个响应将“等待”,直到处理当前事件。

#2


5  

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在浏览器中是单线程的。

#3


0  

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

编辑:删除代码,有些人被挂起来而不是原来的问题

#4


0  

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请求时处理你的队列,我相信会完成你想要的。