Giter Site home page Giter Site logo

thpool's Introduction

Thpool

This is a simple threaded pool worker system that can process tasks in the order they are received.

Example

pool = Thpool.new

pool.perform do
  # ... Action to be enqueued here
end

The worker pool has some sensible defaults as to how many workers will be created, zero when there's no work, up to twenty when there's enough work to be performed. These settings can be customized.

Constructor options:

  • :worker_class - What kind of worker to spawn. Should be a Worker subclass.
  • :workers_min - The minimum number of workers to have running.
  • :workers_max - The maximum number of workers to have running.
  • :count_per_worker - The ratio of tasks to workers.

The default EnThpool::Worker class should suffice for most tasks. If necessary, this can be subclassed. This would be useful if the worker needs to perform some kind of resource initialization before it's able to complete any tasks, such as establishing a database connection.

There is a method after_initialize that will execute on the worker thread immediately after the worker is created. This is useful for performing post-initialization functions that would otherwise block the main thread:

class ExampleDatabaseWorker < Thpool::Worker
  def after_initialize
     # Create a database handle.
     @db = DatabaseDriver::Handle.new

     # Pass in the database handle as the arguments to the blocks being
     # processed.
     @args = [ @db ]
  end
end

It's also possible to re-write the perform method to pass in additional arguments.

If you need to do something immediately before or after processing of a block, two methods are available. As an example this can be used to record the amount of time it took to complete a task:

class ExampleDatabaseWorker < Thpool::Worker
  def before_perform(block)
    @start_time = Time.now
  end

  def after_perform(block)
    puts "Took %ds" % (Time.now - @start_time)
  end
end

If exceptions are generated within the worker thread either because of processing a task or otherwise, these are passed back to the Thpool object via the handle_exception method. The default behavior is to re-raise these, but it's also possible to perform some additional handling here to rescue from or ignore them:

class ExampleDatabasePool < Thpool
  def handle_exception(worker, exception, block = nil)
    # Pass through to a custom exception logger
    ExceptionHandler.log(exception)
  end
end

Copyright

Copyright (c) 2013 Scott Tadman, The Working Group Inc. See LICENSE.txt for further details.

thpool's People

Contributors

tadman avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

thpool's Issues

'SIGUSR1' undeclared

I'm trying to use thpool with MinGW gcc.

I get a couple of errors.

-- Configuring done
-- Generating done
-- Build files have been written to: D:/msys64/home/Projects/test/bin
D:/msys64/home/Projects/test/src/lib/thpool.c: In function 'thpool_pause':
D:/msys64/home/Projects/test/src/lib/thpool.c:249:47: error: 'SIGUSR1' undeclared (first use in this function)
  249 |   pthread_kill(thpool_p->threads[n]->pthread, SIGUSR1);
      |                                               ^~~~~~~
D:/msys64/home/Projects/test/src/lib/thpool.c:249:47: note: each undeclared identifier is reported only once for each function it appears in
D:/msys64/home/Projects/test/src/lib/thpool.c: In function 'thread_do':
D:/msys64/home/Projects/test/src/lib/thpool.c:336:19: error: storage size of 'act' isn't known
  336 |  struct sigaction act;
      |                   ^~~
D:/msys64/home/Projects/test/src/lib/thpool.c:337:2: warning: implicit declaration of function 'sigemptyset' [-Wimplicit-function-declaration]
  337 |  sigemptyset(&act.sa_mask);
      |  ^~~~~~~~~~~
D:/msys64/home/Projects/test/src/lib/thpool.c:340:6: warning: implicit declaration of function 'sigaction' [-Wimplicit-function-declaration]
  340 |  if (sigaction(SIGUSR1, &act, NULL) == -1) {
      |      ^~~~~~~~~
D:/msys64/home/Projects/test/src/lib/thpool.c:340:16: error: 'SIGUSR1' undeclared (first use in this function)
  340 |  if (sigaction(SIGUSR1, &act, NULL) == -1) {
      |                ^~~~~~~
D:/msys64/home/Projects/test/src/lib/thpool.c:336:19: warning: unused variable 'act' [-Wunused-variable]
  336 |  struct sigaction act;
      |                   ^~~
make[2]: *** [CMakeFiles/test.dir/build.make:5491: CMakeFiles/test.dir/src/lib/thpool.c.obj] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/Makefile2:125: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:104: all] Error 2
 [ 0%] Building C object CMakeFiles/test.dir/src/lib/thpool.c.obj

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.