Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Subprocess in python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use Subprocess in python? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

First, subprocess and commonly used wrapper functions to connect documents, Popen does not use wait and communicate

When we run python, we are all creating and running a process. Like the Linux process, a process can fork a child process and have that child process exec another program. In Python, we use the subprocess package in the standard library to fork a child process and run an external program.

Several functions for creating child processes are defined in the subprocess package, and these functions create child processes in different ways, so we can choose one of them to use as needed. In addition, subprocess provides tools for managing standard streams (standard stream) and pipes (pipe) to use text communication between processes.

The subprocess module is a module introduced by python since version 2.4. It is mainly used to replace some old module methods, such as os.system, os.spawn*, os.popen*, commands.* and so on. Subprocess executes external instructions through the child process and obtains the return information of the execution of the child process through the input/output/error pipeline.

The recommended way to call subprocess is to use the run () function for all usage scenarios it can handle. The run () function was added in Python 3.5.If you use it in older versions, you need to download and extend it.

Pip install subprocess.run

Usage

Subprocess.run (args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False,env=None)

Run the command described by args. Wait for the command to complete, and then return an instance of CompletedProcess. Class subprocess.CompletedProcess represents the value returned from run (), indicating the completed process.

The full function form is largely the same as the Popen constructor-- except for timeout, input, and check, all parameters of this function are passed to the Popen interface.

Parameter args

Args is required for all calls and should be a string or a sequence of program parameters list. It is generally preferred to provide a sequence of parameters because it allows the module to handle any required escape and reference parameters (for example, spaces in file names). If you pass a single string, shell must be True (see below), otherwise the string must simply name the program to be executed without specifying any parameters.

Stdin, stdout and stderr

Stdin, stdout, and stderr specify the standard input, standard output, and standard error file handles for the executing program, respectively. Valid values are PIPE, DEVNULL, an existing file descriptor (positive integer), an existing file object, and None. PIPE indicates that a new pipeline should be created for the child process. DEVNULL indicates that the special file os.devnull will be used. With the default setting of None, no redirection occurs; the file handle of the child process inherits from the parent process. In addition, stderr can be STDOUT, indicating that standard error data from child processes should be captured in the same file handle as stdout.

Shell

If shell is True, the specified command is executed through shell. It is useful if you use Python mainly because it provides enhanced control flow that most system shell does not provide, and you still want easy access to other shell features, such as shell pipes, filename wildcards, environment variable extensions, and extension ~ to the user's home directory.

> from subprocess import run > print run ('uname-r') 3.7.0-7-generic > print run (' uname-r'). Stdout3.7.0-7-generic > run ('uname-a') .status0 > print run ('rm not_existing_directory'). Stderrrm: cannot remove `not_existing_directory': No such file or directory > > print run ('ls-la','wc-l') 14 > > print run ('ls-la','wc-lump,'wc-c') 3 > > run ('ls-la'') 'wc-lame,' wc-c') ls-la | wc-l | wc-c > print run ('ls-la'). Stdout.lines ['total 20 user user 4096 Dec 20 22:37 dir','-rw-rw-r-- 1 user user 0 Dec 20 22:52 file']

Just use subprocess.run when executing some instructions. You can specify stdout,stderr,cwd (code work path), env (environment variable)

Def get_path_env (kaldi=DIR.KALDI): # add the environment variable old_path = os.environ ['PATH'] ivectorbin_dir = os.path.join (kaldi,' src', 'ivectorbin') bin_dir = os.path.join (kaldi,' src', 'bin') new_path = "{}: {}: {}" .format (ivectorbin_dir, bin_dir) inside kaldi Old_path) return {"PATH": new_path}

Env = get_path_env (), env = {'PATH':$PATH}.

Sometimes you need to use subprocess.Popen as the input of one process as the output of another process.

Import subprocesschild1 = subprocess.Popen (["cat", "/ etc/passwd"], stdout=subprocess.PIPE) child2 = subprocess.Popen (["grep", "0:0"], stdin=child1.stdout, stdout=subprocess.PIPE) out= child2.communicate () def transform (xvector_scp_fp, test_scp_fp, test_ark_fp, sre16_major_dir=None, sre_combined_dir=None): "" Transform xvector from dimension of 600 to vecotr of dimension 150. The actual dimensions are decided by `transform.mat`. Params: `xvectors_sre_ dir`: The official `xvectors_sre16_ major` directory, which contains `room.vec`; `sre_combined_ dir`: The official `xvectors_sre_ combined` directory, which contains `transform.mat`; `xvector_scp_ fp`: scp file path for xvectors to be transformed; `test_scp_ fp`: location of the destination file path NOTE: Here we generate scp file and ark file, instead of only ark file. The reason is to accommandate the case where a lot of xvectors are invloved. Since xvector scp file is created by default, we don't have to consider when there is not xvector scp file. " Sre16_major_dir = sre16_major_dir or path.join (DIR.CONSTDIR, 'xvectors_sre16_major') sre_combined_dir = sre_combined_dir or path.join (DIR.CONSTDIR,' xvectors_sre_combined') env = get_path_env () # execution path corresponds to line 69 Run_faiss.sh subtract_cmd = ['ivector-subtract-global-mean'] mean_vec_dir = path.join (sre16_major_dir,' mean.vec') subtract_cmd.append (mean_vec_dir) subtract_cmd.append ("scp: {}" .format (xvector_scp_fp)) subtract_cmd.append ("ark:-") p1 = subprocess.Popen (subtract_cmd, stdout=subprocess.PIPE, env=env Stderr=subprocess.DEVNULL) # p1.stdout trans_cmd = ['transform-vec'] trans_mat_fp = path.join (sre_combined_dir,' transform.mat') trans_cmd.append (trans_mat_fp) trans_cmd.append ("ark:-") trans_cmd.append ("ark:-") p2 = subprocess.Popen (trans_cmd, stdin=p1.stdout, stdout=subprocess.PIPE, env=env Stderr=subprocess.DEVNULL) norm_cmd = ['ivector-normalize-length'] norm_cmd.append (' ark:-') dest = "ark,scp: {}, {}" .format (test_ark_fp, test_scp_fp) norm_cmd.append (dest) p3 = subprocess.Popen (norm_cmd, stdin=p2.stdout, stdout=subprocess.PIPE, env=env Stderr=subprocess.PIPE) # wait for p3 to execute rv = p3.communicate () [0] if p3.returncode! = 0: raise XvectorTransformationError (p3.stdout, p3.stderr)

P3.communicate ()

P3.returncode! = 0

Rv.returncode! = 0

Class PldaScoreError (Exception): "" plda score error "" def cmd_err (err_cls): # add alarm error, plda_scores_error def outer (fn): def inner (* args, * * kwargs): rv = fn (* args, * * kwargs) if rv.returncode! = 0: err_msg = rv.stdout + rv.stderr # alarm error message display If type (err_msg) is bytes: err_msg = err_msg.decode () print (err_msg) raise err_cls else: return rv return inner return outer@cmd_err (PldaScoreError) def score (enroll_ark_fp, test_ark_fp, trials_fp, score_fp, log_dir=None, sre16_major_dir=default_sre16 Cwd=DIR.KALDIROOT): # No num_utts.ark file "Params: `sre16_major_ dir`: the directory where `plda_ adapt` locates `enroll_ark_ fp`: enroll ark file path `test_ark_ fp`: test ark file path `trials_ fp`: trials file path `score_ fp`: the file path for the generated score file"log_dir = log_dir or (score_fp +" .log) ") cmd = ['utils/run.pl'] cmd.append (log_dir) cmd.append (" ivector-plda-scoring ") cmd.append ("-- normalize-length=true ") plda_fp = path.join (sre16_major_dir 'plda_adapt') plda_sub_cmd = "ivector-copy-plda-smoothing=0.0 {}-|" .format (plda_fp) cmd.append (plda_sub_cmd) cmd.append ("ark: {}" .format (enroll_ark_fp)) cmd.append ("ark: {}" .format (test_ark_fp)) cmd.append (trials_fp) cmd.append (score_fp) env = get _ path_env () return subprocess.run (cmd Cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # cwd sets the current working directory of the child process Env is used to specify sub-process environment variables. This is the answer to env's question on how to use Subprocess in python. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report