proc
This library is considered production ready.
Utility to create sub process.
from pykit import proc
# execute a shell script
returncode, out, err = proc.shell_script('ls / | grep bin')
print returncode
print out
# output:
# > 0
# > bin
# > sbin
# Or run a command directly.
# Unlike the above snippet, following statement does not start an sh process.
returncode, out, err = proc.command('ls', 'a*', cwd='/usr/local')
# a.py
import sys
with open('foo', 'w') as f:
f.write(str(sys.argv))
# b.py
import time
from pykit import proc
proc.start_daemon('python', './a.py', 'test')
time.sleep(1)
try:
with open('foo', 'r') as f:
print repr(f.read())
except Exception as e:
print repr(e)
syntax:
proc.ProcError(returncode, out, err, cmd, arguments, options)
It is raised if a sub process return code is not 0
.
Besides ProcError.args
, extended from super class Exception
, it has 6
other attributes.
attributes:
ProcError.returncode
: process exit code.ProcError.out
: stdout in one string.ProcError.err
: stderr in one string.ProcError.cmd
: the command a processexec()
.ProcError.arguments
: tuple of command arguments.ProcError.options
: other options passed to this process. Such asclose_fds
,cwd
etc.
syntax:
proc.command(cmd, *arguments, **options)
Run a command
with arguments arguments
in a subprocess.
It blocks until sub process exit.
arguments:
-
cmd
: The path of executable to run. -
arguments
: is tuple or list of arguments passed tocmd
. -
options
: is a dictionary or additional options:-
close_fds
: specifies whether to close all open file descriptor whenfork()
. By default it isTrue
. -
cwd
: specifies working dir of the sub process. By default it isNone
, which means does not change current working dir. -
env
: is a dictionary to pass environment variables to sub process. -
stdin
: is a string used as stdin for sub process. By default it isNone
.
-
return: a 3 element tuple that contains:
returncode
: sub process exit code inint
.out
: sub process stdout in a single string.err
: sub process stderr in a single string.
syntax:
proc.command_ex(cmd, *arguments, **options)
It is the same as proc.command
except that if sub process exit code is not
0, it raises exception proc.ProcError
.
See proc.ProcError
.
return:
a 3 element tuple of returncode
, out
and err
, or raise exception
proc.ProcError
.
syntax:
proc.shell_script(script_str, **options)
It is just a shortcut of:
options['stdin'] = script_str
return command('sh', **options)
syntax:
proc.start_process(cmd, target, env, *args)
Create a child process and replace it with cmd
.
Besides stdin
, stdout
and stderr
, all file
descriptors from parent process will be closed in
the child process. The parent process waits for
the child process until it is completed.
arguments:
-
cmd
: The path of executable to run. Such assh
,bash
,python
. -
target
: The path of the script. -
env
: It is a dictionary to pass environment variables to the child process. -
*args
: Type istuple
orlist
. The arguments passed to the script. Type of every element must bestr
.
return: nothing
Zhang Yanpo (张炎泼) [email protected]
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼) [email protected]