Skip to main content

Easy to use python subprocess interface.

Project description

EasyProcess is an easy to use python subprocess interface.

Links:

Travis Coveralls Latest Version Supported Python versions License Code Health Documentation

Features:
  • layer on top of subprocess module

  • easy to start, stop programs

  • easy to get standard output/error, return code of programs

  • command can be list (preferred) or string (command string is converted to list using shlex.split)

  • logging

  • timeout

  • unit-tests

  • cross-platform, development on linux

  • shell is not supported

  • pipes are not supported

  • stdout/stderr is set only after the subprocess has finished

  • stop() does not kill whole subprocess tree

  • unicode support

  • supported python versions: 2.7, 3.5, 3.6, 3.7, 3.8

  • Method chaining

Similar projects:

Basic usage

>>> from easyprocess import EasyProcess
>>> EasyProcess('python --version').call().stderr
u'Python 2.6.6'

Installation

install:

pip install EasyProcess

uninstall:

pip uninstall EasyProcess

Usage

Simple example

Example program:

#-- include('examples/hello.py')--#
from easyprocess import EasyProcess
import sys

s = EasyProcess([sys.executable, '-c', 'print "hello"']).call().stdout
print(s)
#-#

Output:

#-- sh('python -m easyprocess.examples.hello')--#
hello
#-#

General

The command can be a string list or a concatenated string:

#-- include('examples/cmd.py')--#
from easyprocess import EasyProcess

print('-- Run program, wait for it to complete, get stdout (command is string):')
s=EasyProcess('python -c "print 3"').call().stdout
print(s)

print('-- Run program, wait for it to complete, get stdout (command is list):')
s=EasyProcess(['python','-c','print 3']).call().stdout
print(s)

print('-- Run program, wait for it to complete, get stderr:')
s=EasyProcess('python --version').call().stderr
print(s)

print('-- Run program, wait for it to complete, get return code:')
s=EasyProcess('python --version').call().return_code
print(s)

print('-- Run program, wait 1 second, stop it, get stdout:')
s=EasyProcess('ping localhost').start().sleep(1).stop().stdout
print(s)

#-#

Output:

#-- sh('python -m easyprocess.examples.cmd')--#
-- Run program, wait for it to complete, get stdout (command is string):
3
-- Run program, wait for it to complete, get stdout (command is list):
3
-- Run program, wait for it to complete, get stderr:
Python 2.7.6
-- Run program, wait for it to complete, get return code:
0
-- Run program, wait 1 second, stop it, get stdout:
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.034 ms
#-#

Shell commands

Shell commands are not supported.

return_code

EasyProcess.return_code is None until EasyProcess.stop or EasyProcess.wait is called.

With

By using with statement the process is started and stopped automatically:

from easyprocess import EasyProcess
with EasyProcess('ping 127.0.0.1') as proc: # start()
    # communicate with proc
    pass
# stopped

Equivalent with:

from easyprocess import EasyProcess
proc = EasyProcess('ping 127.0.0.1').start()
try:
    # communicate with proc
    pass
finally:
    proc.stop()

Timeout

This was implemented with “daemon thread”.

“The entire Python program exits when only daemon threads are left.” http://docs.python.org/library/threading.html:

#-- include('examples/timeout.py')--#
from easyprocess import EasyProcess

s = EasyProcess('ping localhost').call(timeout=2).stdout
print(s)
#-#

Output:

#-- sh('python -m easyprocess.examples.timeout')--#
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.025 ms
#-#

Replacing existing functions

Replacing os.system:

retcode = os.system("ls -l")
==>
p = EasyProcess("ls -l").call()
retcode = p.return_code
print(p.stdout)

Replacing subprocess.call:

retcode = subprocess.call(["ls", "-l"])
==>
p = EasyProcess(["ls", "-l"]).call()
retcode = p.return_code
print(p.stdout)

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

EasyProcess-0.2.8.tar.gz (29.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

EasyProcess-0.2.8-py2.py3-none-any.whl (10.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file EasyProcess-0.2.8.tar.gz.

File metadata

  • Download URL: EasyProcess-0.2.8.tar.gz
  • Upload date:
  • Size: 29.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.17

File hashes

Hashes for EasyProcess-0.2.8.tar.gz
Algorithm Hash digest
SHA256 da7f67a006e2eb63d86a8f3f4baa9d6752dab9676009a67193a4e433f2f41c2a
MD5 2f41d0b795671e648ffb8e54bbe9e31b
BLAKE2b-256 9e07d8895f0908e9424c1a782617e4c1b45d935936e58798140e3e649d05165a

See more details on using hashes here.

File details

Details for the file EasyProcess-0.2.8-py2.py3-none-any.whl.

File metadata

  • Download URL: EasyProcess-0.2.8-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.17

File hashes

Hashes for EasyProcess-0.2.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e8258e8fafddc97a9780bafa83a0b7a7223e3679478ebc7a64cb320d434354e5
MD5 c7aaab6946a3969910aca62d601ee61f
BLAKE2b-256 7e6357755fadd28c989d578862f1ed7995348ed666333e03cdab6f6ea3f26ab0

See more details on using hashes here.

Supported by

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