Cmd¶
-
class
pydu.cmd.
TimeoutExpired
(cmd, timeout, output=None, stderr=None)¶ This exception is raised when the timeout expires while waiting for a child process.
- Attributes:
- cmd, output, stdout, stderr, timeout
-
pydu.cmd.
run
(cmd, wait=True, env=None, shell=False, timeout=None, timeinterval=1)¶ Run cmd based on
subprocess.Popen
.If
wait
is True,run
will return the tuple of(returncode, stdout)
. Note,stderr
is redirected tostdout
. Ifwait
is False,run
will return object ofPopen
.shell
is same to parameter ofPopen
.If the process does not terminate after
timeout
seconds, aTimeoutExpired
exception will be raised.timeinterval
is workable when timeout is given on Python 2. It means process status checking interval.The child process is not killed if the timeout expires, so in order to cleanup properly a well-behaved application should kill the child process and finish communication.
>>> from pydu.cmd import run >>> run('echo hello') (0, b'hello\r\n') # Python 3 >>> run('echo hello', wait=False) <subprocess.Popen at 0x22e4010f9e8>
-
pydu.cmd.
run_with_en_env
(cmd, wait=True, shell=False, env=None, timeout=None, timeinterval=1)¶ Run cmd with English character sets environment, so that the output will be in English. Parameters are same with
run
.
-
pydu.cmd.
terminate
(pid)¶ Terminate process by given
pid
.On Windows, using kernel32.TerminateProcess to kill. On other platforms, using os.kill with signal.SIGTERM to kill.
-
pydu.cmd.
cmdline_argv
()¶ Get command line argv of self python process. On Windows when using Python 2,
cmdline_argv
is implemented by usingshell32.GetCommandLineArgvW
to get sys.argv as a list of Unicode strings.On other platforms or using Python 3,
cmdline_argv
is same tosys.argv
.>>> from pydu.cmd import cmdline_argv >>> cmdline_argv() ['/Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py', '61253', '61254']