🚨 Active Supply Chain Attack:node-ipc Package Compromised.Learn More
Socket
Book a DemoSign in
Socket

parenting

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parenting

bundlerRubyGems.org
Version
0.1.8
Version published
Maintainers
1
Created
Source

Parenting

Sometimes you have a lot of stuff to do, and you want to get it done fast. What's the obvious thing to do? Have a bunch of kids, and tell them to do it, of course!

Intent

This gem allows a straightforward model of multiprocessing: spin off multiple external programs to do work, and then wait for them to finish. That use-case is already possible in a basic way using spawn, but that gives you no control or interactivity with the spawned process.

Because you may have more tasks than can reasonably run simultaneously (memory restrictions, processor count, etc), you usually would like to specify an upper-limit on how many of those tasks may be running simultaneously. If you have chores that are disproportionately long, you usually want to minimize the net run-time - Parenting allows you to specify a 'cost' for each chore, which it will use to sort them into longest-job first (provably minimzing the net run-time).

The most important detail is that your chores will probably want to do some kind of logging, so that your main process can tell what is going on in each of them. The natural way to do this is to allow the external processes to log via stderr, and to do something with each line of log so produced - each chore takes a callable for how to thread-safely handle that output.

You can pass input to the child process via the :stdin option, but it is not intended for bulk interaction - that string is fed to the process immediately, and the pipe is then closed.

Thread-Safety

Parenting uses threads internally to allow jobs to finish in arbitrary order. None of the callbacks you initialize chores with will be used outside of the main thread however, and all data structures passed into the options hash will be dup'd, so you can safely reuse them for multiple chores.

Usage


# build a coordinator that allows 4 children at a time
boss = Parenting::Boss.new(4)

['ls', 'ls -l', 'ls -a', 'echo hello'].each do |cmd|
  boss.add_chore({
    :command    => cmd,
    :on_success => lambda { STDERR.puts "#{cmd} succeeded" },
    :on_failure => lambda { STDERR.puts "#{cmd} failed"    },
    :on_stderr  => lambda { |ln| STDERR.puts "#{cmd} produced: #{ln}" }
  })
end

boss.run!

FAQs

Package last updated on 19 Feb 2015

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