Skip to content

Instantly share code, notes, and snippets.

@we4tech
Last active December 16, 2015 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save we4tech/5487442 to your computer and use it in GitHub Desktop.
Save we4tech/5487442 to your computer and use it in GitHub Desktop.
Thread pool, and separate mongo client with each thread.
class ThreadPool
attr_accessor :size, :pool
def logger
BeatDeckMachine::logger
end
def initialize(size)
@size = size
@jobs = Queue.new
@pool = create_pool
end
def create_pool
@size.times.map do |i|
Thread.start do
Thread.current[:id] = i
catch(:thread_quit) do
session = create_mongo_client
loop do
job, args = @jobs.pop
args << session
logger.debug "Thread(#{Thread.current[:id]}): Handling job"
job.call *args
end
end
end
end
end
def join
#@pool.map(&:join)
# This cause Deadlock ? exceptino from rb_check_deadlock
# To avoid this situation i've used another thread to check whether all threads from pool are dead already?
# Example -
# Thread.new do
# break until pool.pool.select(&:alive?).size.zero?
# end.join
end
def shutdown
logger.info 'Shutting down threads.'
@size.times do
add { throw :thread_quit }
end
@pool.map &:join
end
def add(*args, &job)
logger.debug 'Added new job in queue'
@jobs << [job, args]
end
def queue_size;
@jobs.size
end
def create_mongo_client
# Some stuffs here with Moped::Session.new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment