Sha256: ac7e8417905a64f4f83a9f9ce303c765bf299f318876210bcb0ad5261c86c41d

Contents?: true

Size: 888 Bytes

Versions: 1

Compression:

Stored size: 888 Bytes

Contents

require "parallel"
require "etc"
require "rainbow"

module Convert2Ascii
  class MultiTasker
    def initialize(proc_tasks)
      @proc_tasks = proc_tasks
      @count = set_threads_count
      @finished = []
      @time_start = nil
    end

    def set_threads_count
      cpu_threads = (Etc.nprocessors || 1)
      cpu_threads = cpu_threads > 4 ? cpu_threads - 2 : 1
      cpu_threads
    end

    def progress(index)
      @finished << index
      print(Rainbow("\rprocessing...  #{sprintf("%.2f", (1.0 * @finished.length / @proc_tasks.length) * 100)} % (time: #{sprintf("%.2f", Time.now - @time_start)} s)").green)
    end

    def run
      @time_start = Time.now
      results = Parallel.map(@proc_tasks, in_processes: @count, finish: ->(item, index, result) { progress(index) }, finish_in_order: true) do |task|
        task && task.call
      end

      results
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
convert2ascii-0.2.1 lib/convert2ascii/multi-tasker.rb