ã¾ããã
ä»é±ã® Ruby Weekly ã® Featured ã«ä¸ãã£ã¦ããã©ã¤ãã©ãªã試ããã
https://github.com/beraboris/eldritch
ããã¯ä½
éåæå¦çãDSLã©ã¤ã¯ã«æ¸ããã©ã¤ãã©ãªãéåæå¦çã¨ãã£ã¦ãæ ¼å¥½ããThreadãä½ã£ã¦ããã ãã
ä½ããããããã®ã
æ¸ãæ¹ããããããã以ä¸ã®ããã«æ¸ããã修飾åã¿ããã
async def task # ... end
ãã㯠Ruby 2.1 ãã def
ãã¡ã½ããåãã·ã³ãã«ã§è¿ãããã«ãªã£ãããåºæ¥ãè¨æ³ã§ãä»å¾ã¯ Eldritch ã®ããã«ä¿®é£¾åã®ãããªè¨æ³ã使ãã©ã¤ãã©ãªãåºã¦ãããã
private def securet; end
ã§åãã§ããå ´åãããªãã£ãã
使ã£ã¦ã¿ã
# ç¯å²ãéå®ãããå ´å㯠`eldritch/safe` ã使ã require "eldritch" class Task def initialize(name) @name = name end def execute puts "execute: %s" % @name end end # éåæã¡ã½ããã®å®ç¾© # å®æ 㯠`__async_generate` ã¨ããã¡ã½ãããå®ç¾©ãã¦ã # å ã®ã¡ã½ããã `async` ãããã¯ã§å²ã£ã¦ send ãã¦ããã async def generate(name) loop do task = Task.new(name) $tasks << task sleep rand(0.5) end end $tasks = [] # together ã§å²ã㨠Eldritch::Group ãçæããã # ã°ã«ã¼ãã«å¯¾ã㦠join ã interrupt ãå¼ã¶ã¨ # æå±ãããã¹ã¦ã® Thread ã«åæã«éããã together do |group| # éåæã¡ã½ããã®å¼ã³åºã generate("command 1") generate("command 2") generate("command 3") generate("command 4") generate("command 5") # ãããã¯ã§ãéåæã¿ã¹ã¯ãä½ããã¨ãã§ãã # å®éã«ã¯ Thread ãä½ã£ã¦ `call` ãã¦ããã ã # èªåçã«ç¾å¨ã®ã°ã«ã¼ãã«è¿½å ããã async do 10.times do sleep 0.1 task = $tasks.pop next if task.nil? task.execute end # ã°ã«ã¼ãã«å¯¾ã㦠`interrupt` ãå¼ãã§ããã®ã§ã # ãã® Thread *以å¤* ã®åãã°ã«ã¼ãã® Thread ãæ¢ã¾ãã group.interrupt end end
$ ruby main.rb execute: command 5 execute: command 4 execute: command 4 execute: command 1 execute: command 3 execute: command 3 execute: command 3 execute: command 2 execute: command 1 execute: command 5
ä¸å¦ççãªææ³
- Thread ã使ã£ã¦ããã®ã§ãã¾ãå®ç¨çã§ã¯ãªãããè¨æ³ãã¨ã¦ããããããã£ãã
- ã³ã¼ããã·ã³ãã«ãªã®ã§èªã¿ãããã£ãã
- ãããã Celluloid ã«ããã¨ããããã