This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SyncCyclicIterator | |
| def initialize(ary) | |
| @ary = ary | |
| @mtx = Mutex.new | |
| @idx = 0 | |
| end | |
| def next | |
| ret = nil | |
| @mtx.synchronize do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ExResourceLock | |
| def initialize(map) | |
| @rmap = map | |
| @mtx = Mutex.new | |
| @cv = ConditionVariable.new | |
| @mtx.synchronize do | |
| @free_resources = @rmap.keys | |
| @locked_resources = [] | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ConcurrentExecutor | |
| def concurrent_each(concurrency, queue_size, &proc) | |
| require "thread" | |
| queue = SizedQueue.new(queue_size) | |
| queueing_thread = Thread.start{ | |
| each{|item| queue.enq([item, true]) } | |
| concurrency.times{ queue.enq([nil, false]) } # send termination signal | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CyclicIterator | |
| def initialize(collection) | |
| @collection = collection | |
| @i = 0 | |
| end | |
| def next | |
| item = nil | |
| item = @collection[@i] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # usage: ruby ./random-kun.rb aaa bbb ccc ddd | |
| srand | |
| puts ARGV[rand(ARGV.size)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.HashMap; | |
| import java.util.Iterator; | |
| import java.util.Map; | |
| import java.util.UUID; | |
| import java.util.concurrent.LinkedBlockingQueue; | |
| public class Channel<E> { | |
| private final Map<UUID, LinkedBlockingQueue<E>> channels = new HashMap<UUID, LinkedBlockingQueue<E>>(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # to parse datetime expression like: "2012-01-01 24:00:01" | |
| def my_time_parse(str) | |
| y, m, d, hour, min, sec = str.split(/[^0-9]/).map{|x| x.to_i } | |
| base = Time.local(y, m, d) | |
| return (base + (hour*60*60 + min*60 + sec)) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pickle | |
| def hoge(n): | |
| n2 = n * 2 | |
| print "this is hoge, n*2=%d" % n2 | |
| s_hoge = pickle.dumps(hoge) | |
| loaded_hoge = pickle.loads(s_hoge) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # using https://github.com/tomotaka/msgpack-rpc-python/commit/9a116f7255a5d6c2f773ee48225cd05173b7916b | |
| import tornado.web | |
| import tornado.gen | |
| import tornado.httpserver | |
| import tornado.ioloop | |
| import msgpackrpc | |
| import msgpackrpc.loop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # perform async MessagePack RPC request | |
| # base idea is from: https://github.com/msgpack/msgpack-rpc-python/pull/5 | |
| import tornado.web | |
| import tornado.gen | |
| import tornado.httpserver | |
| import tornado.ioloop | |
| import msgpackrpc | |
| import msgpackrpc.loop |
OlderNewer