Giter Club home page Giter Club logo

runcmd's Introduction

RunCmd

RunCmd is ia Python module which allows you to run a command with a timeout option.

Introduction

RunCmd can be considered a substitute for a common subprocess usage pattern:

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
ret, out = p.communicate()

But with additional option to set a timeout for the command. Like subprocess.Popen, this is a blocking call, and will wait for either the command to complete or a timeout to occur.

RunCmd is not meant to be a replacement for subprocess module itself.

Compatibility

  • supports Python 2.6x and Python 2.7x.
  • runs under both Windows and Linux. It should run under OSX.

How to run

Installation

Just copy the the runcmd.py file into your directory. Or use the setup.py if you wish to install it.

Example Code

RunCmd has two methods: RunCmd.run() and RunCmd.run_fd().

RunCmd.run() reads the output into am internal buffer and return the output to the user as a tuple of (returncode, output).

from runcmd import RunCmd

cmd = RunCmd()
#run the command using the shell and a timeout of 5 seconds.
returncode, out = cmd.run('echo Hello World', shell=True, timeout=5)
print "returncode is %d. Output is %s" % (returncode, out)

RunCmd.run() is not suitable for handling processes with large volume of data as it attempts to buffer the entire output. Instead, useRunCmd.run_fd(), which allows a user to pass in a file object where the output will be written into.

from runcmd import RunCmd

with open('tmp.txt', 'wb') as f:
    cmd = RunCmd()
    # run the command using the shell and a timeout of 5 seconds.
    cmd.run_fd('echo Hello World', f, shell=True, timeout=5)

print "returncode is %d. Output is %s" % (cmd.returncode, open('tmp.txt', 'rb').read())

RunCmd.run_fd can handle large volume of data; there is a utility script under ./util/generate_output.py which can simulate output of different sizes. Try simulating a 1GB output :-).

RunCmd has a CLI as well. For example,

python runcmd.py --cmd="echo Hello World" --shell --timeout=5

Testing

RunCmd has a unittests script. See ./tests/test_runcmd.py.

License

RunCmd is released under the MIT license. See LICENSE.txt

runcmd's People

Contributors

wschang avatar

Watchers

cloud123 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.