SoFunction
Updated on 2024-10-30

Python subprocess subpocess principle and usage analysis

Python's sub-processing is, well, the use of python to open a sub-process (which of course seems like a load of crap), but it may not be quite the same as what we understand.

I: How is it understood?

What we might understand: start an extra process to run a certain python function (use the multiprocessing package if you only want to do this)

Correct understanding: python through the shell/cmd to open a new program process, not limited to python functions, for example, we can open a "ls" command process to list the files in the current folder, this "ls This "ls" command is obviously a generic shell function, not a python function.

function:

# Open the subprocess and run "ls". Output files in current folder <br data-filtered="filtered">import subprocess
p = (["ls"])

II. How is it used?

When we want to simply use subprocess to open a process to run a python function, we even have to do it in a roundabout way:

Let's say it's like this:

(1) Create a new function script that needs to be run test_print.py

import sys
def print_it(a, b , c):
  print(a)
  print(b)
  print(c)
if __name__ == "__main__":
  print_it([1], [2], [3])

(2) Create another script and run test_print.py by passing it parameters

import subprocess


p = (["python", "test_print.py", "a1", "b2", "c3"])
pp = (["python", "test_print.py", "d4", "e5", "f6"])

(3) Output results:

a1

b2

c3

d4

e5

f6

III: Some Simple Uses

1. Let's say the output is redirected:

(1) Still create a new function script that needs to be run test_print.py

import sys
def print_it(a, b , c):
  print(a)
  print(b)
  print(c)
if __name__ == "__main__":
  print_it([1], [2], [3])

(2) Create another script and run test_print.py by passing it parameters

import subprocess

p = (["python", "test_print.py", "a1", "b2", "c3"], stdout=, shell=True) #shell=True is required, otherwise stdout cannot be read.
pp = (["python", "test_print.py", "d4", "e5", "f6"], stdout=, shell=True) 

print(()) 
print(())

At this point, however, the output results in a binary file

b'a1\r\nb2\r\nc3\r\n'
b'd4\r\ne5\r\nf6\r\n'

We need to deal with this (of course it's fine if you don't, it just looks awkward)

import subprocess
p = (["python", "test_print.py", "a1", "b2", "c3"], stdout=, shell=True) #shell=True is required, otherwise stdout cannot be read.
pp = (["python", "test_print.py", "d4", "e5", "f6"], stdout=, shell=True) 
# Just convert it with str.
print(str((), encoding = "utf8"))
print(str((), encoding = "utf8"))

(3) Orientation to external documents

import subprocess
# Note that this step is required
f_handler=open('', 'w')
p = (["python", "test_print.py", "a1", "b2", "c3"], stdout=f_handler)
pp = (["python", "test_print.py", "d4", "e5", "f6"], stdout=f_handler)# A misuse
p_error = (["python", "test_print.py", "d4", "e5", "f6"], stdout='') # It won't work.

We'll notice that nothing will be displayed on the screen, the output has already been imported into it

This is the entire content of this article.