简体   繁体   English

有没有Queue的替代方法,可让您一次.pop多个结果?

[英]Is there an alternative to Queue that lets you .pop more than one result at a time?

I'm using Ruby's Queue on my Sinatra server to send tasks to a worker thread to then deal with them and save them in a database of my choosing. 我在Sinatra服务器上使用Ruby的Queue将任务发送到工作线程,然后对其进行处理并将其保存在我选择的数据库中。 One of the databases happens to be Redis, which supports some really cool pipelinening . 数据库之一是Redis,它支持一些非常酷的流水线化

My question is: is there a way to modify the Queue class so that .pop can pop more than one item at a time like normal Array#pop , à la [1,2,3,4,4,5].pop(3) , so that I can pipeline the results? 我的问题是:有没有一种方法可以修改Queue类,以便.pop可以像普通Array#pop一样一次弹出多个项目,例如[1,2,3,4,4,5].pop(3) ,这样我就可以流水线结果了?

Also, what happens to a Heroku instance if a Queue becomes too large? 另外,如果队列太大,Heroku实例会怎样? A warning, and a restart - some lost data, correct? 警告,然后重新启动-一些丢失的数据,对吗?

How about: 怎么样:

def pop_queue(q, n)
  ary = []
  n.times { ary << q.pop }
  ary
end

You don't want to monkey-patch a core library. 您不想猴子修补核心库。 If other code calls it and isn't aware of the change in arity or parameter type, your overall application will blow up. 如果其他代码调用它,并且不知道Arity或参数类型的更改,则整个应用程序将崩溃。

...if your data becomes too large? ...如果您的数据太大? You are the programmer. 你是程序员。 Aren't you supposed to program defensively to avoid those situations? 您不是应该为避免这种情况而进行防御性编程吗?

If you have a possible situation where you could use all available space, then write your code to not suck up RAM and instead use a database. 如果您可能会使用所有可用空间,请编写代码以免占用RAM,而使用数据库。 At a minimum use a disk-based SQLite database and something like Sequel to talk to it. 至少要使用基于磁盘的SQLite数据库和类似Sequel的数据库。

The beauty of a database is multiple threads can talk to it, just don't read and write to the same row. 数据库的优点是多个线程可以与之交谈,只是不要在同一行中读写。 Some DBMs support row locking and others support table locking. 一些DBM支持行锁定,而其他DBM支持表锁定。 You'll have to write your code to take advantage of that and treat the table like your queue. 您必须编写代码来利用它,并像对待队列一样对待表。

If you want the best performance, use a message queue, like AMQP or RabbitMQ instead of the database, but that's starting to talk about more complex code and bigger hosting iron. 如果要获得最佳性能,请使用消息队列(例如AMQP或RabbitMQ)代替数据库,但这开始是在谈论更复杂的代码和更大的托管对象。

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

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