• ruby脚本示例代码


    if __FILE__ == $0
      $:.unshift File.join(File.dirname(__FILE__), '..')
      require 'eventmachine'
      # TODO: real tests
      # TODO: pass in one object instead of two? .each{ |iter| puts iter.current; iter.next }
      # TODO: support iter.pause/resume/stop/break/continue?
      # TODO: create some exceptions instead of using RuntimeError
      # TODO: support proc instead of enumerable? EM::Iterator.new(proc{ return queue.pop })
      EM.run{
        EM::Iterator.new(1..50).each{ |num,iter| p num; iter.next }
        EM::Iterator.new([1,2,3], 10).each{ |num,iter| p num; iter.next }
        i = EM::Iterator.new(1..100, 5)
        i.each(proc{|num,iter|
          p num.to_s
          iter.next
        }, proc{
          p :done
        })
        EM.add_timer(0.03){
          i.concurrency = 1
        }
        EM.add_timer(0.04){
          i.concurrency = 3
        }
        EM::Iterator.new(100..150).map(proc{ |num,iter|
          EM.add_timer(0.01){ iter.return(num) }
        }, proc{ |results|
          p results
        })
        EM::Iterator.new(%w[ pwd uptime uname date ], 2).inject({}, proc{ |hash,cmd,iter|
          EM.system(cmd){ |output,status|
            hash[cmd] = status.exitstatus == 0 ? output.strip : nil
            iter.return(hash)
          }
        }, proc{ |results|
          p results
        })
      }
    end
  • 相关阅读:
    Linux系统教程:设置GRUB菜单密码
    vimdiff的常用命令
    Zero-Copy实现原理
    解决业务代码里的分布式事务一致性问题
    用好这6个APP,学英语SO EASY!
    线程池调优
    理解select,poll,epoll实现分析
    时序图
    性能监控-TP理解
    sshd_config OpenSSH SSH 进程配置文件配置说明
  • 原文地址:https://www.cnblogs.com/lexus/p/1935328.html
Copyright © 2020-2023  润新知