Git Product home page Git Product logo

bashthreads's Introduction

Bash multi-threading library

Copyright (c) 2014 Jess Mahan

This simple to use library provides multi-threading capability for your bash scripts in a posix threads style.

Getting Started

Include this into your script:

#!/bin/bash
source ~/threads.sh

Library Functions

thread_init

Initialize the library, limiting the maximum thread count to Defining max threads is important, otherwise you could potentially fork bomb yourself.

This function MUST be called ONLY ONCE, and BEFORE any other thread calls

Create no more than this many threads

thread_create

Create a single thread of with

thread_wait should be called immediately after this.

A command to run in a new thread / process Arguments to pass to the command

thread_wait (final)

Block until we are no longer at

When called with an argument (such as "thread_wait final"), blocks parent until all threads are complete

Example:

In this example, we have a file called "hosts.txt" which contains a single hostname per line. We then create 64 simulaneous worker threads to each resolve the hostname and then exit.

#!/bin/bash

. ./threads.sh

function nslkup {
     dig +short $1
}
thread_init 64
for i in $(cat hosts.txt)
do
  thread_create nslkup ${i}
  if [ $? -eq 1 ]; then
    thread_wait
    thread_create nslkup ${i}
  fi
done
thread_wait final

bashthreads's People

Contributors

alienrobotarmy avatar

Stargazers

Dongsu Lee avatar leewish avatar Theodore Omtzigt avatar Norton avatar

Watchers

James Cloos avatar  avatar

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.