Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple_thread_pool

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple_thread_pool

  • 1.0.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

SimpleThreadPool

Simple implementation of the thread pool to manage executing tasks in parallel. The thread pool implemented by this code is designed to allow throttled, parallel execution of tasks.

Unlike some thread pools, this code does not maintain an internal queue of tasks. Instead, it simply blocks until a thread is available. This ensures that the pool memory size doesn't become bloated with 1000's of enqueued blocks of code.

The threads created for running the tasks are short lived threads and are not reused by the pool. This ensures that no thread local variables can accidentally be reused between threads.

Installation

Add this line to your application's Gemfile:

gem 'simple_thread_pool'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_thread_pool

Usage

# Create a pool of 10 threads to work with.
thread_pool = SimpleThreadPoolnew(10)

# Execute a block of code in a thread.
# If there are no free threads in the pool, then this will block until one is freed up.
thread_pool.execute do
  # Do some work here
end

# Execute a block of code with an identifier.
# The thread pool will not run code with the same identifier in parallel
# and will execute them in the order they are called
thread_pool.execute("foo") do
  # Do some work here
end

# Block until all threads have finished executing.
# You should always call this method after all calls to `execute` have been made
thread_pool.finish

Error handling.

All error handling must be conducted inside the execute block. The main thread will not be notified of any exceptions. You can use the synchronized method on the thread pool if you need to work data from the main thread.

Example of how you can track any errors in a shared array.

errors = []

thread_pool.execute do
  begin
    # Do something
  rescue Error => e
    thread_pool.synchronized { errors << e }
    raise e
  end
end

thread_pool.finish

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bdurand/simple_thread_pool.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 08 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc