将同步http请求/响应模型与基于异步队列的模型连接起来的好方法是什么?
当用户的HTTP请求到来时,它会生成一个进入队列的工作请求(在本例中为beanstalkd).其中一名工人接受请求,完成工作并准备回复.
队列模型不是请求/响应 - 只有请求,而不是响应.所以问题是,我们如何才能最好地将响应反馈回HTTP世界并返回给用户?
思路:
Beanstalkd支持轻量级主题或队列(他们称之为管).我们可以为每个请求创建一个管,让工作人员在该管上创建一条消息,让http进程坐下并等待管的响应.不要特别喜欢这个,因为它有apache进程坐着记忆.
让http客户端轮询响应.用户的初始HTTP请求将启动队列上的作业并立即返回.客户端(用户的浏览器)定期轮询响应.在后端,工作人员将其响应放入memcached,我们将nginx连接到memcached,因此轮询很轻.
使用Comet.类似于第二个选项,但与更高级的http通信,以避免轮询.
我倾向于2,因为它很容易且很清楚(我还没有用过彗星).我猜我可能还有一个更好的模型,我没有想过.你怎么看?
假设我有一个滚动的值集合,我指定集合的大小,并且每当添加新值时,超出此指定大小的任何旧值都将被删除.显然(我已经测试了这个)用于此行为的最佳集合类型是队列:
myQueue.Enqueue(newValue)
If myQueue.Count > specifiedSize Then myQueue.Dequeue()
Run Code Online (Sandbox Code Playgroud)
但是,如果我想计算队列中第一个和最后一个项目之间的差异怎么办?显然我无法通过索引访问项目.但是,从队列切换到实现IList的东西看起来有点矫枉过正,就像写一个新的类Queue类一样.现在我有:
Dim firstValue As Integer = myQueue.Peek()
Dim lastValue As Integer = myQueue.ToArray()(myQueue.Count - 1)
Dim diff As Integer = lastValue - firstValue
Run Code Online (Sandbox Code Playgroud)
那个叫ToArray()我困扰的电话,但是一个更好的选择不会来找我.有什么建议?
我绑定了ListBox一个Queue<string>.当我将项目入队/出列时,ListBox不会更新.
我有帮助入队/出队以提高财产变化
protected void EnqueueWork(string param)
{
Queue.Enqueue(param);
RaisePropertyChanged("Queue");
}
protected string DequeueWork()
{
string tmp = Queue.Dequeue();
RaisePropertyChanged("Queue");
return tmp;
}
Run Code Online (Sandbox Code Playgroud) 我之前在Python中使用过多线程库,但这是我第一次尝试在C中进行线程化.我想创建一个工作池.反过来,这些工作者应该推送或从队列中弹出.以下代码还没有完成,但是到目前为止我做的是:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUMTHREADS 20 /* number of threads to create */
typedef struct node node;
typedef struct queue queue;
struct node {
char *name;
node *next;
};
struct queue {
node *head;
node *tail;
};
/* pop: remove and return first name from a queue */
char *pop(queue *q)
{
if (q->head == NULL)
return NULL;
char *name = q->head->name;
node *tmp = q->head;
q->head = q->head->next;
free(tmp);
return name;
}
/* push: add …Run Code Online (Sandbox Code Playgroud) 我需要实现一种任务缓冲区.基本要求是:
我正在考虑使用如下的队列来实现它.希望得到有关实施的反馈.还有其他更明智的想法来实现这样的事情吗?
public class TestBuffer
{
private readonly object queueLock = new object();
private Queue<Task> queue = new Queue<Task>();
private bool running = false;
public TestBuffer()
{
}
public void start()
{
Thread t = new Thread(new ThreadStart(run));
t.Start();
}
private void run()
{
running = true;
bool run = true;
while(run)
{
Task task = null;
// Lock queue before doing anything
lock (queueLock)
{
// If the queue is currently empty and it is …Run Code Online (Sandbox Code Playgroud) 我有一个涉及迭代优先级队列的java赋值.队列由带有字符串和int的对象组成,我需要有一种方法来检查队列中所有对象的单独对象的字符串.
这是一个迭代器对象的最佳方法吗?这看起来太乱了.我可以出列队并入队,但这似乎效率低下.也许是一个foreach循环?
如果我有一个heapq,其中包含一些元素,如:
import heapq
class Element(object):
def __init__(self, name, val):
self.name = name
self.val = val
if __name__ == "__main__":
heap = []
e1 = Element('A', 1)
e2 = Element('B', 65)
e3 = Element('C', 53)
e4 = Element('D', 67)
...
heapq.heappush(heap, e1)
heapq.heappush(heap, e2)
heapq.heappush(heap, e3)
heapq.heappush(heap, e4)
...
#IF I want to take elements from the heap and print them I will call:
while heap:
new_e = heapq.heappop(heap)
print new_e.name + ' ' + str(new_e.val)
Run Code Online (Sandbox Code Playgroud)
假设我在堆上有50个元素.我想将元素e3的值从val = 53更改为val = 0.所以这不是堆的顶部元素.我也不想从堆中删除其他元素.我该怎么做这样的更新?
我有以下用例:多个客户端推送到共享Redis列表.单独的工作进程应该排空此列表(处理和删除).Wait/multi-exec到位以确保,这很顺利.
出于性能原因,我不想立即调用'drain'进程,但是在x毫秒之后,从第一个客户端推送到(然后为空)列表的那一刻开始.
这类似于分布式下划线/ lodash 去抖动功能,计时器在第一个项目进入时开始运行(即:'领先'而不是'尾随')
我正在寻找以容错方式可靠地执行此操作的最佳方法.
目前我倾向于以下方法:
NX和px方法.这允许:
nx参数的用途px参数的用途1如果可以设置该值,则返回此命令,这意味着之前不存在任何值.0否则返回.A 1表示当前客户端是第一个运行该进程的客户端,因为Redis列表已耗尽.因此,这在纸上起作用,但感觉有点复杂.是否有其他方法可以以分布式容错方式工作?
顺便说一句:Redis和分布式队列已经到位,所以我不认为将它用于此问题是一个额外的负担.
我在Laravel 5中设置了一个队列来删除公司和相关记录.每次发生这种情况时,后端都会发生很多工作,所以队列是我最好的选择.
我将config/queue.php文件与我的文件一起设置,.env以便使用database驱动程序.我正在使用该Queue::pushOn方法将作业推送到一个名为的队列company_deletions.防爆.
Queue::pushOn('company_deletions', new CompanyDelete($id));
Run Code Online (Sandbox Code Playgroud)
CompanyDelete命令在哪里创建php artisan command:make CompanyDelete --queued
我试图使用以下命令让我的队列进行处理:
php artisan queue:work
php artisan queue:work company_deletions
php artisan queue:listen
php artisan queue:listen company_deletions
php artisan queue:work database
php artisan queue:listen database
Run Code Online (Sandbox Code Playgroud)
有时在查看上述命令的输出时,我收到以下错误:
[InvalidArgumentException]
No connector for []
Run Code Online (Sandbox Code Playgroud)
即使我没有收到错误,我也无法通过某种原因实际处理这些工作.当我查看我的jobs表时,我可以看到队列中的作业,但是attempts列显示0,reserved显示0并且reserved_at为空.我错过了一些步骤吗?我已经多次查看了文档,并且不能为我的生活弄清楚出了什么问题.我也没有在laravel错误日志中看到任何内容.什么会阻止这些作业进入jobs数据库后进行处理?任何帮助表示赞赏.
我写了一个使用std :: queue的小程序
queue<double> the_queue;
for(int i = 0; i < 100; i++)
{
for(int j = 0; j < 5000; j++)
{
double d = 1.0 * (rand() % 1000);
the_queue.push(d);
}
}
printf("Done pushing\n");
while(!the_queue.empty())
{
the_queue.pop();
}
printf("Done popping\n");
Run Code Online (Sandbox Code Playgroud)
我将2个断点放在printf("Done pushing\n");并且printf("Done popping\n");在命中断点时检查程序的内存使用情况(在任务管理器中显示).在Done pushing,内存使用量约为34 MB,但Done popping内存使用量仍为~34 MB.这让我感到惊讶!
为什么是这样?有没有办法克服这个问题?