Coder Social home page Coder Social logo

xpool's Introduction

OVERVIEW

Project XPool
Homepage https://github.com/robgleeson/xpool
Documentation http://rubydoc.info/github/robgleeson/xpool/frames
CI Build Status
Author Rob Gleeson

DESCRIPTION

XPool is a lightweight process pool. The pool manages a group of subprocesses that are used when the pool is asked to dispatch a 'unit of work'. A 'unit of work' is defined as any object that implements the run method.

All subprocesses in the pool have their own message queue that the pool places work onto according to a very simple algorithm: the subprocess who has scheduled the least amount of work is the subprocess who will have the unit of work put onto its message queue. The message queue that each subprocess has is also what ensures work can be queued when the pool becomes dry (all subprocesses are busy).

EXAMPLES

The examples don't demonstrate everything that XPool can do. The API docs cover the missing pieces.

1.

A demo of how to schedule a unit of work:

#
# Make sure you define your units of work before
# you create a process pool or you'll get strange
# serialization errors.
#
class Unit
  def run
    sleep 1
  end
end
pool = XPool.new 3
pool.schedule Unit.new
pool.shutdown

2.

A demo of how to run a single unit of work across all subprocesses in the pool:

class Unit
  def run
    puts Process.pid
  end
end
pool = XPool.new 5
pool.broadcast Unit.new
pool.shutdown

3.

A demo of how you can interact with subprocesses through XPool::Process objects:

class Unit
  def run
    sleep 1
  end
end
pool = XPool.new 2
subprocess = pool.schedule Unit.new 
p subprocess.busy? # => true

4.

A demo of how to resize the pool from 5 to 2 subprocesses at runtime:

pool = XPool.new 5
pool.resize! 1..2
pool.shutdown

5.

A demo of how to gracefully shutdown but force a hard shutdown when 3 seconds pass by & all subprocesses have not exited:

class Unit
  def run
    sleep 5
  end
end
pool = XPool.new 5
pool.schedule Unit.new
pool.shutdown 3

DEBUGGING OUTPUT

XPool can print helpful debugging information if you set XPool.debug to true:

XPool.debug = true

Or you can temporarily enable debugging output for the duration of a block:

XPool.debug do 
  pool = XPool.new 5
  pool.shutdown
end

The debugging information you'll see is all about how the pool is operating. It can be interesting to look over even if you're not bug hunting.

SIGUSR1

All XPool managed subprocesses define a signal handler for the SIGUSR1 signal. A unit of work should never define a signal handler for SIGUSR1 because that would overwrite the handler defined by XPool. SIGUSR2 is not caught by XPool and it could be a good second option.

INSTALL

$ gem install xpool

LICENSE

MIT. See LICENSE.txt

xpool's People

Contributors

havenwood avatar

Watchers

Morrigain H avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.