Git Product home page Git Product logo

machma's Introduction

Status badge for tests

machma - Easy parallel execution of commands with live feedback

Introduction

In order to fully utilize modern machines, jobs need to be run in parallel. For example, resizing images sequentially takes a lot of time, whereas working on multiple images in parallel makes much better use of a multi-core CPU and therefore is much faster. This tool makes it very easy to execute tasks in parallel and provides live feedback. In case of errors or lines printed by the program, the messages are tagged with the job name.

machma by default reads newline-separated values and replaces all command-line arguments set to {} with the file name. The number of jobs is set to the number of cores for the CPU of the host machma is running on.

Sample Usage

Resize all images found in the current directory and sub-directories to 1200x1200 pixel at most:

$ find . -iname '*.jpg' | machma --  mogrify -resize 1200x1200 -filter Lanczos {}

The command specified after the double dash (--) is executed with each parameter that is set to {} replaced with the file name. At the bottom, a few status lines are printed after a summary line. The lines below visualize the status of the instances of the program running in parallel. The line for an instance will either contain the name of the file (in this case) that is being processed followed by the newest message printed by the program.

demo: resizing files

Ping a large number of hosts, but only run two jobs in parallel:

$ cat /tmp/ips | machma -p 2 -- ping -c 2 -q {}

The program ping will exit with an error code when the host is not reachable, and machma prints an error message for all jobs which returned an error code.

demo: ping hosts

A slightly more sophisticated (concerning shell magic) example is the following, which does the same but recduces the output printed by ping a lot:

$ cat /tmp/ips | machma -- sh -c 'ping -c 2 -q $0 > /dev/null && echo alive' {}

demo: ping hosts again

Using --timeout you can limit the time mogrify is allowed to run per picture. (Prevent jobs from 'locking up') The value for timeout is formatted in golang time.Duration format. When the timeout is reached the program gets canceled.

$ find . -iname '*.jpg' | machma --timeout 5s --  mogrify -resize 1200x1200 -filter Lanczos {}

Files With Spaces

Sometimes filenames have spaces, which may be problematic with shell commands. Most of the time, this should not be a problem at all, since machma runs programs directly (using the execve syscall on Linux for example) instead of using system(). For all other cases there's the --null (short: -0) option which instructs machma to read items separated by null bytes from stdin. This can be used with the option -print0 of the find command like this:

$ find . -iname '*.jpg' -print0 | machma --null --  mogrify -resize 1200x1200 -filter Lanczos {}

Installation

Installation is very easy, install a recent version of Go and run:

$ go run build.go

Afterwards you can view the online help:

$ ./machma --help
Usage of ./machma:
      --no-id              hide the job id in the log
      --no-name            hide the job name in the log
      --no-timestamp       hide the time stamp in the log
  -0, --null               use null bytes as input separator
  -p, --procs int          number of parallel programs (default 2)
      --replace string     replace this string in the command to run (default "{}")
      --timeout duration   set maximum runtime per queued job (0s == no limit)

machma's People

Contributors

fd0 avatar mattn avatar peterbe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

machma's Issues

Hangs without TTY

I'm trying to use machma to speed up some operations in build scripts, but it always seems to hang without a TTY (which is the case when my build scripts normally run). I have reproduced this behavior inside of a docker container simply by including or omitting the -t flag.

This will hang:

$ docker run --rm golang:1.9 /bin/bash -c 'go get -u github.com/fd0/machma ; find /go -iname "*.go" | machma -- md5sum {}'

But this will not hang:

$ docker run -t --rm golang:1.9 /bin/bash -c 'go get -u github.com/fd0/machma ; find /go -iname "*.go" | machma -- md5sum {}'

The only difference in the commands is the -t flag.

{} is replaced only when separated by spaces

Here's an example of how I would like to use machma to replace parallel in my environment.

find . -name "*.png" | machma -- convert {} {}.jpg

Another way I've been experimenting with quotes and slashes to see if I get it to be interpreted correctly is by feeding it to echo.
find . -name "*.png" | machma -- echo {} {}.jpg

The expected result is that the original file would remain in place and a new one with the ".jpg" extension would be created.

What I get is a single file named "{}.jpg".

Arguments to control output

It would be nice if the output could be controlled by using options.

The worker number and timestamp either contain data I don't need or are already provided by the data being output from my other process. These could be "off' by default and enabled with options or "on" by default and disabled with options.

It might also be nice to have an argument that accepts a keyword to act as a filter. Grepping is so easy that maybe it wouldn't be worth the effort but then the nice colorized output is lost.

Inconsistent output

$ cat foo.sh
#!/bin/sh
echo $1
$ for a in {1..10}; do for b in {1..100}; do echo $b; done | go run github.com/fd0/machma@latest -- ./foo.sh "{}" | wc -l; done
95
101
101
101
99
102
100
101
100
100

One would expect the same number of output lines on each run: 100.

Comparing two runs:

$ doit() { sort <(for b in {1..100}; do echo $b; done | go run github.com/fd0/machma@latest --no-id --no-timestamp -- ./foo.sh "{}") }
$ diff -U0 <(doit) <(doit)
--- /proc/self/fd/11	2021-12-31 02:37:51.409602436 +0100
+++ /proc/self/fd/12	2021-12-31 02:37:51.409602436 +0100
@@ -30,0 +31 @@
+35 35
@@ -33 +33,0 @@
-38 38
@@ -52,0 +53,2 @@
+56 56
+57 57
@@ -66,0 +69 @@
+71 71

Status area "jumpy"

The number of lines in the status area is variable so reading and keeping track of the processed/failed/workers area is difficult.

An example to see what I'm talking about is to run find / | machma -p 9 -- ls -alh {} and try to keep an eye on the "processed" count.

Maybe there's a better solution but what I was thinking: adding empty lines so that the status area stays in the same location. So there would always be number of lines equal to the maximum workers count plus one.

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.