lib/noda/rqueue.rb in noda-0.0.13 vs lib/noda/rqueue.rb in noda-0.0.14
- old
+ new
@@ -1,53 +1,76 @@
# -*- utf8 -*-
module Noda
require 'monitor'
-# ƒWƒ‡ƒu‚̃Lƒ…[ŽÀ‘•
-# ƒLƒ…[‚̓XƒŒƒbƒhƒZ[ƒt‚É‘‚¢‚Ä‚¢‚é
+# ジョブã®ã‚ュー実装
+# ã‚ューã¯ã‚¹ãƒ¬ãƒƒãƒ‰ã‚»ãƒ¼ãƒ•ã«æ›¸ã„ã¦ã„ã‚‹
+#
+# ã‚ューã‹ã‚‰å€¤ã‚’å–り出ã™ã¨ã€ã‚ューã«ã¯æ®‹ã‚‰ãªã„。
+# ==使用法
+# require 'noda'
+# server =Noda::JobServer.new
+# q = server.input #<= Jobサーãƒãƒ¼ãŒæŒã£ã¦ã‚‹
+# q.push Noda::MyTask.new("hogehgoe")
#
-#
class RQueue
include DRb::DRbUndumped
attr_reader :name
+ #
def initialize( max=nil, name=nil )
@name = name
@list = []
@max = nil
@max = max if max
self.extend(MonitorMixin)
@m_empty = self.new_cond
@m_full = self.new_cond
end
+ # ã‚ュー末尾ã«ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã‚’è¿½åŠ ã€‚
+ #
+ # ã‚ュー満æ¯æ™‚ã¯å®Ÿè¡Œã‚¹ãƒ¬ãƒƒãƒ‰ã‚’Waitã•ã›ã¾ã™ã€‚
def push obj
self.synchronize{
@m_full.wait_while{ self.full? } if @max
@list.push obj
@m_empty.broadcast
}
end
+ # ã‚ュー先é ã‹ã‚‰ã‚ªãƒ–ジェクトをå–り出ã™ï¼Ž
+ #
+ # ã‚ュー空ãªã‚‰å®Ÿè¡Œã‚¹ãƒ¬ãƒƒãƒ‰ã‚’Waitã•ã›ã‚‹ï¼Ž
def pop
self.synchronize{
@m_empty.wait_while{ self.empty? }
obj = @list.shift
@m_full.broadcast if @max
obj
}
end
+ # 実験用メソッド・使ã‚ãªã„.
def include?(v) @list.include? v end
alias exists? include?
+ # ã‚ューã®å…ˆé N個をå–り出ã™ï¼Ž
def firsts(n=1) (0...n).map{self.pop} end
+ # ã‚ューã®å…ˆé 1個をå–り出ã™ï¼Ž pop ã®åˆ¥å
def first() self.pop end
+ # ã‚ューã®ã‚µã‚¤ã‚ºã‚’å–å¾—
def size() @list.size end
+ # ã‚ãƒ¥ãƒ¼æ ¼ç´å¯èƒ½æ•°
def max_size() @max end
+ # ã‚ューã«å€¤ãŒã‚ã‚‹ã‹
def empty?() @list.empty? end
+ # ã‚ュー満æ¯ãŒè¿‘ã„ã¨ãã«Trueã‚’è¿”ã™ï¼Ž
def close_to_full?() @list.size >= @max-5 end
+ # ã‚ューãŒæº€æ¯ã‹ã©ã†ã‹
def full?()
- # max=nil ‚È‚çLimitlessB‚‚܂薳ŒÀ‘å
+ # max=nil ãªã‚‰Limitless。ã¤ã¾ã‚Šç„¡é™å¤§
@list.size >= @max if @max
end
+ # ã‚ューã®å€¤å…¨ã¦ã‚’å–り出ã™ï¼Ž
def all() self.firsts(self.size) end
- # ‚P‚©‚ç‚m‚Ü‚Å‚Ì’l‚ðŽæ‚éB‚OŽn‚Ü‚è‚Å‚È‚¢‚±‚Æ‚É’ˆÓ
+ # ã‚ューã®å…ˆé N番目ã®å€¤ã‚’調ã¹ã‚‹ï¼Žãƒã‚§ãƒƒã‚¯ç”¨ã€‚
+ # - pos 1ã‹ã‚‰ï¼®ã¾ã§ã®å€¤ã‚’å–る。先é ã¯1 ã§æŒ‡å®šã™ã‚‹ï¼Žï¼å§‹ã¾ã‚Šã§ãªã„ã“ã¨ã«æ³¨æ„
def _at(pos)
i = pos - 1
return @list.at(i) if (i) < self.size
end
end
\ No newline at end of file