Skip to content

Wrapper library for Thread class for easily describing parallel processing.

Notifications You must be signed in to change notification settings

arimay/thread-task

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Thread::Task

Yet another, a wrapper library of Thread class for easily describing parallel processing.

Features

  • Thread::Task is a configuration that allows you to briefly describe the configuration often used in parallel processing.

  • Thread::Pool limits the number of parallel executions of Thread::Task.

  • Using Kernel#future method, asynchronous process can be described simply.

Installation

Add this line to your application’s Gemfile:

gem 'thread-task'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install thread-task
or
$ gem install -l thread-task-x.x.x.gem

Usage

Start parallel execution, wait for completion and get result.

require  "thread/task"

task  =  ::Thread::Task.new do
  sleep  2
  Time.now
end
p task.value

Simple expression.

require  "thread/task/future"

task  =  future do
  sleep  2
  Time.now
end
p task.value

Limit the max count of parallel executions.

require  "thread/task/future"

pool  =  ::Thread::Pool.new(3)

tasks  =  (0...10).map do |i|
  future( i, pool: pool ) do |j|
    p ["-->", j]
    sleep( rand * 2 )
    p ["<--", j]
  end
end

tasks.each(&:join)

Reference

Create a new Thread::Task.

Thread::Task.new( *args, pool: nil, &block )
Kernel#future( *args, pool: nil, &block )

Kernel#future method can be used with require "thread/task/future".

  • Result:

    • Thread::Task object.

  • Parameter:

    • args: Pass args as is to the block.

    • pool: Thread::Pool object. (default: nil)

    • block: callback action.

  • Block Parameter:

    • args: Treat args as local variables specific to that thread.

Wait for thread stop. Ignore thread exceptions.

Thread::Task#join
  • Result:

    • nil

  • Parameter:

    • none.

Wait for the thread to stop and get the result. Detect the thread exception.

Thread::Task#value
  • Result:

    • The execution result of the block.

  • Parameter:

    • none.

Cancel the execution of the thread.

Thread::Task#cancel
  • Result:

    • nil.

  • Parameter:

    • none.

Create a new Thread::Pool.

Thread::Pool.new( count )
  • Result:

    • Thread::Pool object.

  • Parameter:

    • count: Max count of parallel executions.

Acquire a resource from pool.

Thread::Pool#acquire
  • Result:

    • nil.

  • Parameter:

    • none.

Release a resource to pool.

Thread::Pool#release
  • Result:

    • nil.

  • Parameter:

    • none.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/arimay/thread-task.

License

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

Copyright (c) ARIMA Yasuhiro <[email protected]>

About

Wrapper library for Thread class for easily describing parallel processing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published