Skip to main content

Run multiple subprocesses asynchronous/in parallel with streamed output/non-blocking reading. Also various tools to replace shell scripts.

Project description

shelljob

This provides a clean way to execute subprocesses, either one or multiple in parallel, capture their output and monitor progress:

  • Single sub process call with optional timeout

  • High level FileMonitor to execute several processes in parallel and store output in a file

  • Low level Group execution to execute jobs in parallel and capture output

Additional tools for working with the filesystem are also included:

  • find which offers much of the functionality of the shell find utility

  • shelljob.fs.NamedTempFile provides a with block wrapper for temporary named files

    NOTE: Only the Python 3 form is now maintained. The Python 2 is still included for backwards compatibility but will not likely get new features.

Parallel Subprocesses

Using the Job system is the quickest approach to just run processes and log their output (by default in files named ‘/tmp/job_ID.log’)

from shelljob import job

jm = job.FileMonitor()
jm.run([
    [ 'ls', '-alR', '/usr/local' ],
    'my_prog',
    'build output input',
])

An array will passed directly to subprocess.Popen, a string is first parsed with shlex.split.

The lower level Group class provides a simple container for more manual job management.

from shelljob import proc

g = proc.Group()
p1 = g.run( [ 'ls', '-al', '/usr/local' ] )
p2 = g.run( [ 'program', 'arg1', 'arg2' ] )

while g.is_pending():
    lines = g.readlines()
    for proc, line in lines:
        sys.stdout.write( "{}:{}".format( proc.pid, line ) )

Simple Subprocess calls

A simplified call function allows timeouts on subprocesses and easy acces to their output.

from shelljob import proc

# capture the output
output = proc.call( 'ls /tmp' )
# this raises a proc.Timeout exception
proc.call( 'sleep 10', timeout = 0.1 )

Find

The ‘find’ funtion is a multi-faceted approach to generating listings of files.

from shelljob import fs

files = fs.find( '/usr/local', name_regex = '.*\\.so' )
print( "\n".join(files) )

Refer to the API docs for all parameters. Just let me know if there is some additional option you need.

Issues

You can use my Launchpad project to submit issues.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shelljob-0.5.5.tar.gz (228.8 kB view details)

Uploaded Source

File details

Details for the file shelljob-0.5.5.tar.gz.

File metadata

  • Download URL: shelljob-0.5.5.tar.gz
  • Upload date:
  • Size: 228.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for shelljob-0.5.5.tar.gz
Algorithm Hash digest
SHA256 7c72e84bcb2f9874d8ecf232be94aafcbca65872197334c559db8d19769ed904
MD5 277518d2beccce79c91d13d0e6e5d3da
BLAKE2b-256 4907be0c7f5633af4eee1cfa4edbe214019737ebf7a49f0da29d14be7e8d463f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page