Hướng dẫn python send data to running process - python gửi dữ liệu đến quá trình đang chạy

Trước hết, tôi khá mới với Python. Tôi cũng đã tìm kiếm một giải pháp, nhưng tôi đoán cách tiếp cận thông thường (Subprocess.popen) đã giành được công việc trong trường hợp của tôi.

Show

Tôi phải chuyển các đối số cho người nghe trong một tập lệnh Python đã chạy mà không cần bắt đầu tập lệnh nhiều lần. Có một ví dụ làm thế nào để chuyển tin nhắn cho màn hình LCD:

function printMsgText(message_text)
local f = io.popen("home/test/show_message.py '" .. message_text .. "'")
end

Bản thảo LUA này ở trên xác định quá trình được gọi là mỗi khi nhận được tin nhắn. Quá trình được gọi là (show_message.py) trông như thế:

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])

Tôi cần một cái gì đó tương tự, ngoại trừ có một tập lệnh khác đang chạy ở backround, vì vậy show_message.py không phải là quá trình cuối cùng mà cần truyền đối số/tin nhắn cho một tập lệnh khác, đã chạy. Ý tưởng của tôi là chỉ để show_message.py in tin nhắn vào bảng điều khiển và sử dụng sys.argv trong quá trình chính nhưng tôi hơi sợ rằng nó có thể bị lộn xộn.

Có cách nào dễ dàng để làm điều này? Trân trọng

Chỉnh sửa:

Kịch bản chính là kiểm soát một động cơ bước. Dựa trên đầu vào của người dùng, động cơ điều khiển một số bước được xác định trước. Một phần của tập lệnh đang chờ đầu vào của người dùng trông như thế này:

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break

Bây giờ tôi cần vượt qua vị trí mong muốn thông qua một ứng dụng web được thiết kế theo cách tôi mô tả ở trên (ví dụ: gọi một bản thảo LUA mỗi khi biến/đối số được truyền) và không thông qua bảng điều khiển.

Mã nguồn: lib/sub -crocess.py Lib/subprocess.py


Mô -đun

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
1 cho phép bạn sinh ra các quy trình mới, kết nối với đường ống đầu vào/đầu ra/lỗi của chúng và có được mã trả lại của chúng. Mô -đun này dự định thay thế một số mô -đun và chức năng cũ hơn:

Thông tin về cách mô -đun

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
1 có thể được sử dụng để thay thế các mô -đun và chức năng này có thể được tìm thấy trong các phần sau.

Xem thêm

PEP 324 - PEP đề xuất mô -đun phụ – PEP proposing the subprocess module

Tính khả dụng: Không phải emscripten, không phải wasi.: not Emscripten, not WASI.

Mô -đun này không hoạt động hoặc không có sẵn trên các nền tảng Webassugging

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
3 và
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
4. Xem các nền tảng Webassugging để biết thêm thông tin.WebAssembly platforms for more information.

Sử dụng mô -đun >>> subprocess.run(["ls", "-l"]) # doesn't capture output CompletedProcess(args=['ls', '-l'], returncode=0) >>> subprocess.run("exit 1", shell=True, check=True) Traceback (most recent call last): ... subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1 >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True) CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0, stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'') 1

Cách tiếp cận được đề xuất để gọi các quy trình con là sử dụng chức năng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 cho tất cả các trường hợp sử dụng mà nó có thể xử lý. Đối với các trường hợp sử dụng nâng cao hơn, giao diện
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 cơ bản có thể được sử dụng trực tiếp.

SubProcess.run (args, *, stdin = none, input = none, stdout = none, stderr = none, capture_output = false, shell = false, cwd = none Không, Text = none, env = none, Universal_Newlines = none, ** other_popen_kwargs) ¶run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)

Chạy lệnh được mô tả bởi Args. Đợi lệnh hoàn thành, sau đó trả lại một thể hiện

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
8.

Các đối số được hiển thị ở trên chỉ là những đối số phổ biến nhất, được mô tả dưới đây trong các đối số được sử dụng thường xuyên (do đó việc sử dụng ký hiệu chỉ từ khóa trong chữ ký viết tắt). Chữ ký chức năng đầy đủ phần lớn giống như hàm xây dựng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 - hầu hết các đối số cho hàm này được chuyển qua giao diện đó. (Thời gian chờ, đầu vào, kiểm tra và capture_output không.)Frequently Used Arguments (hence the use of keyword-only notation in the abbreviated signature). The full function signature is largely the same as that of the
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 constructor - most of the arguments to this function are passed through to that interface. (timeout, input, check, and capture_output are not.)

Nếu Capture_output là đúng, stdout và stderr sẽ bị bắt. Khi được sử dụng, đối tượng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 bên trong được tự động tạo bằng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
1 và
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
2. Các đối số stdout và stderr có thể không được cung cấp cùng lúc với Capture_output. Nếu bạn muốn chụp và kết hợp cả hai luồng thành một, hãy sử dụng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
1 và
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
4 thay vì Capture_output.

Đối số thời gian chờ được chuyển đến

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5. Nếu thời gian chờ hết hạn, quá trình trẻ em sẽ bị giết và chờ đợi. Ngoại lệ
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 sẽ được nuôi dưỡng lại sau khi quá trình con đã chấm dứt.

Đối số đầu vào được chuyển đến

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 và do đó cho quá trình phụ stdin. Nếu được sử dụng, nó phải là một chuỗi byte hoặc một chuỗi nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản là đúng. Khi được sử dụng, đối tượng
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 nội bộ được tự động tạo bằng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
9 và đối số Stdin cũng không được sử dụng.

Nếu kiểm tra là đúng và quy trình thoát ra với mã thoát khác không, ngoại lệ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 sẽ được nêu ra. Các thuộc tính của ngoại lệ đó giữ các đối số, mã thoát và stdout và stderr nếu chúng bị bắt.

Nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản là đúng, các đối tượng tệp cho stdin, stdout và stderr được mở ở chế độ văn bản bằng cách sử dụng mã hóa và lỗi được chỉ định hoặc mặc định

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1. Đối số Universal_Newlines tương đương với văn bản và được cung cấp cho khả năng tương thích ngược. Theo mặc định, các đối tượng tệp được mở ở chế độ nhị phân.

Nếu env không phải là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2, thì đó phải là một ánh xạ xác định các biến môi trường cho quy trình mới; Chúng được sử dụng thay vì hành vi mặc định của việc kế thừa môi trường quy trình hiện tại. Nó được chuyển trực tiếp đến
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7. Ánh xạ này có thể được STR trên bất kỳ nền tảng hoặc byte nào cho byte trên các nền tảng POSIX giống như
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
4 hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
5.

Examples:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')

Mới trong phiên bản 3.5.

Đã thay đổi trong phiên bản 3.6: Đã thêm thông số mã hóa và lỗiAdded encoding and errors parameters

Đã thay đổi trong phiên bản 3.7: Đã thêm tham số văn bản, như một bí danh dễ hiểu hơn của Universal_Newlines. Đã thêm tham số Capture_output.Added the text parameter, as a more understandable alias of universal_newlines. Added the capture_output parameter.

ClassSubProcess.completedProcess¶subprocess.CompletedProcess

Giá trị trả về từ

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6, đại diện cho một quá trình đã kết thúc.

args¶

Các đối số được sử dụng để khởi chạy quá trình. Đây có thể là một danh sách hoặc một chuỗi.

ReturnCode¶

Trạng thái thoát của quá trình trẻ em. Thông thường, trạng thái thoát là 0 chỉ ra rằng nó đã chạy thành công.

Giá trị âm

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
7 chỉ ra rằng đứa trẻ bị chấm dứt bởi tín hiệu
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 (chỉ POSIX).

Stdout¶

Đã bắt được stdout từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 được gọi với mã hóa, lỗi hoặc văn bản = true.
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 nếu stdout không bị bắt.

Nếu bạn chạy quy trình với

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1, stdout và stderr sẽ được kết hợp trong thuộc tính này và
Popen(['/bin/sh', '-c', args[0], args[1], ...])
2 sẽ là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2.

stderr¶

Đã bắt Stderr từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 được gọi với mã hóa, lỗi hoặc văn bản = true.
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 Nếu Stderr không bị bắt.

Check_returnCode ()()

Nếu

Popen(['/bin/sh', '-c', args[0], args[1], ...])
6 là không khác, hãy tăng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0.

Mới trong phiên bản 3.5.

quá trình con.devnull¶DEVNULL

Giá trị đặc biệt có thể được sử dụng làm đối số STDIN, STDOUT hoặc STDERR thành

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 và chỉ ra rằng tệp đặc biệt
Popen(['/bin/sh', '-c', args[0], args[1], ...])
9 sẽ được sử dụng.

Mới trong phiên bản 3.3.

quá trình con.pipe¶PIPE

Giá trị đặc biệt có thể được sử dụng làm đối số stdin, stdout hoặc stderr thành

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 và chỉ ra rằng một đường ống vào luồng tiêu chuẩn nên được mở. Hữu ích nhất với
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5.

quá trình con.stdout¶STDOUT

Giá trị đặc biệt có thể được sử dụng làm đối số STDERR thành

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 và chỉ ra rằng lỗi tiêu chuẩn sẽ đi vào cùng một tay cầm như đầu ra tiêu chuẩn.

ngoại lệ subprocess.SubprocessError

Lớp cơ sở cho tất cả các trường hợp ngoại lệ khác từ mô -đun này.

Mới trong phiên bản 3.3.

quá trình con.pipe¶ subprocess.TimeoutExpired

Giá trị đặc biệt có thể được sử dụng làm đối số stdin, stdout hoặc stderr thành

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 và chỉ ra rằng một đường ống vào luồng tiêu chuẩn nên được mở. Hữu ích nhất với
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5.

quá trình con.stdout¶

Giá trị đặc biệt có thể được sử dụng làm đối số STDERR thành

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 và chỉ ra rằng lỗi tiêu chuẩn sẽ đi vào cùng một tay cầm như đầu ra tiêu chuẩn.

ngoại lệ

Lớp cơ sở cho tất cả các trường hợp ngoại lệ khác từ mô -đun này.

ngoại lệ

Lớp con của

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3, được nâng lên khi hết thời gian chờ trong khi chờ đợi một quá trình trẻ em.

Stdout¶

Đã bắt được stdout từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 được gọi với mã hóa, lỗi hoặc văn bản = true.
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 nếu stdout không bị bắt.

stderr¶

Đã bắt Stderr từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 được gọi với mã hóa, lỗi hoặc văn bản = true.
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 Nếu Stderr không bị bắt.

Mới trong phiên bản 3.3.

quá trình con.pipe¶stdout and stderr attributes added

Giá trị đặc biệt có thể được sử dụng làm đối số stdin, stdout hoặc stderr thành
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 và chỉ ra rằng một đường ống vào luồng tiêu chuẩn nên được mở. Hữu ích nhất với
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5.
subprocess.CalledProcessError

quá trình con.stdout¶

ReturnCode¶

Trạng thái thoát của quá trình trẻ em. Thông thường, trạng thái thoát là 0 chỉ ra rằng nó đã chạy thành công.

quá trình con.stdout¶

Giá trị đặc biệt có thể được sử dụng làm đối số STDERR thành

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 và chỉ ra rằng lỗi tiêu chuẩn sẽ đi vào cùng một tay cầm như đầu ra tiêu chuẩn.

ngoại lệ

Lớp cơ sở cho tất cả các trường hợp ngoại lệ khác từ mô -đun này.

Stdout¶

Đã bắt được stdout từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 được gọi với mã hóa, lỗi hoặc văn bản = true.
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 nếu stdout không bị bắt.

stderr¶

Đã bắt Stderr từ quá trình trẻ em. Trình tự byte hoặc chuỗi nếu

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 được gọi với mã hóa, lỗi hoặc văn bản = true.
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 Nếu Stderr không bị bắt.

quá trình con.pipe¶stdout and stderr attributes added

Giá trị đặc biệt có thể được sử dụng làm đối số stdin, stdout hoặc stderr thành >>> subprocess.run(["ls", "-l"]) # doesn't capture output CompletedProcess(args=['ls', '-l'], returncode=0) >>> subprocess.run("exit 1", shell=True, check=True) Traceback (most recent call last): ... subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1 >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True) CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0, stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'') 7 và chỉ ra rằng một đường ống vào luồng tiêu chuẩn nên được mở. Hữu ích nhất với Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."]) 5.

quá trình con.stdout¶

Giá trị đặc biệt có thể được sử dụng làm đối số STDERR thành

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 và chỉ ra rằng lỗi tiêu chuẩn sẽ đi vào cùng một tay cầm như đầu ra tiêu chuẩn.

ngoại lệ

Nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản (còn được gọi là Universal_Newlines) là đúng, các đối tượng tệp STDIN, STDOUT và STDERR sẽ được mở ở chế độ văn bản bằng cách sử dụng mã hóa và lỗi được chỉ định trong cuộc gọi hoặc mặc định cho

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1.

Đối với STDIN, các ký tự kết thúc dòng

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
10 trong đầu vào sẽ được chuyển đổi thành dấu phân cách dòng mặc định
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
11. Đối với stdout và stderr, tất cả các kết thúc dòng trong đầu ra sẽ được chuyển đổi thành
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
10. Để biết thêm thông tin, hãy xem tài liệu của lớp
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1 khi đối số dòng mới cho hàm tạo của nó là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2.

Nếu chế độ văn bản không được sử dụng, stdin, stdout và stderr sẽ được mở dưới dạng các luồng nhị phân. Không có chuyển đổi mã hóa hoặc kết thúc dòng được thực hiện.

Mới trong phiên bản 3.6: Đã thêm các tham số mã hóa và lỗi.Added encoding and errors parameters.

Mới trong phiên bản 3.7: Đã thêm tham số văn bản dưới dạng bí danh cho Universal_Newlines.Added the text parameter as an alias for universal_newlines.

Nếu shell là

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00, lệnh được chỉ định sẽ được thực thi thông qua shell. Điều này có thể hữu ích nếu bạn đang sử dụng Python chủ yếu cho luồng điều khiển nâng cao mà nó cung cấp trên hầu hết các vỏ hệ thống và vẫn muốn truy cập thuận tiện vào các tính năng vỏ khác như ống shell, ký tự đại diện, mở rộng biến đổi môi trường và mở rộng
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
16 cho nhà của người dùng danh mục. Tuy nhiên, lưu ý rằng bản thân Python cung cấp việc triển khai nhiều tính năng giống như vỏ (đặc biệt là
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
17,
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
18,
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
19,
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
20,
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
21 và
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
22).

Đã thay đổi trong phiên bản 3.3: Khi Universal_Newlines là

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00, lớp sử dụng mã hóa
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
24 thay vì
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
25. Xem lớp
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1 để biết thêm thông tin về thay đổi này.When universal_newlines is
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00, the class uses the encoding
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
24 instead of
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
25. See the
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1 class for more information on this change.

Các tùy chọn này, cùng với tất cả các tùy chọn khác, được mô tả chi tiết hơn trong tài liệu xây dựng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7.

Constructor Popen Constructor¶

Việc tạo và quản lý quy trình cơ bản trong mô -đun này được xử lý bởi lớp

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7. Nó cung cấp rất nhiều sự linh hoạt để các nhà phát triển có thể xử lý các trường hợp ít phổ biến hơn không được bao phủ bởi các chức năng tiện lợi.

classSubProcess.popen (args, bufsize = -1, thực thi = none, stdin = none, stdout = none, stderr = none . = None, pipesize = -1, process_group = none) ¶subprocess.Popen(args, bufsize=- 1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, group=None, extra_groups=None, user=None, umask=- 1, encoding=None, errors=None, text=None, pipesize=- 1, process_group=None)

Thực hiện một chương trình trẻ em trong một quy trình mới. Trên Posix, lớp sử dụng hành vi giống như ____ 129 để thực hiện chương trình trẻ em. Trên Windows, lớp sử dụng chức năng Windows

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
30. Các đối số cho
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 như sau.

Args nên là một chuỗi các đối số chương trình hoặc một đối tượng giống như một chuỗi hoặc giống như đường dẫn. Theo mặc định, chương trình thực thi là mục đầu tiên trong args nếu args là một chuỗi. Nếu Args là một chuỗi, việc giải thích phụ thuộc vào nền tảng và được mô tả dưới đây. Xem các đối số shell và thực thi để biết sự khác biệt bổ sung so với hành vi mặc định. Trừ khi có quy định khác, nên truyền ARGS như một chuỗi.path-like object. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.

Cảnh báo

Để có độ tin cậy tối đa, hãy sử dụng một đường dẫn đủ điều kiện cho thực thi. Để tìm kiếm một tên không đủ tiêu chuẩn trên

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
32, hãy sử dụng
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
33. Trên tất cả các nền tảng, việc vượt qua
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
34 là cách được đề xuất để khởi chạy lại trình thông dịch Python hiện tại và sử dụng định dạng dòng lệnh
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
35 để khởi chạy mô-đun đã cài đặt.
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
32, use
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
33. On all platforms, passing
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
34 is the recommended way to launch the current Python interpreter again, and use the
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
35 command-line format to launch an installed module.

Việc giải quyết đường dẫn thực thi (hoặc mục đầu tiên của Args) phụ thuộc vào nền tảng. Đối với POSIX, xem

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
29 và lưu ý rằng khi giải quyết hoặc tìm kiếm đường dẫn thực thi, CWD ghi đè thư mục làm việc hiện tại và ENV có thể ghi đè biến môi trường
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
32. Đối với Windows, hãy xem tài liệu của các tham số
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
38 và
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
39 của WinAPI
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
40 và lưu ý rằng khi giải quyết hoặc tìm kiếm đường dẫn thực thi với
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
41, CWD không ghi đè lên thư mục làm việc hiện tại và Env không thể ghi đè lên biến đổi môi trường
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
32. Sử dụng một đường dẫn đầy đủ tránh tất cả các biến thể này.

Một ví dụ về việc chuyển một số đối số cho một chương trình bên ngoài như một chuỗi là:

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])

Trên Posix, nếu Args là một chuỗi, chuỗi được hiểu là tên hoặc đường dẫn của chương trình để thực thi. Tuy nhiên, điều này chỉ có thể được thực hiện nếu không chuyển các đối số cho chương trình.

Ghi chú

Có thể không rõ ràng làm thế nào để chia một lệnh shell thành một chuỗi các đối số, đặc biệt là trong các trường hợp phức tạp.

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
43 có thể minh họa cách xác định mã thông báo chính xác cho ARGS:

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!

Lưu ý cụ thể rằng các tùy chọn (chẳng hạn như -input) và các đối số (như trứng.txt) được phân tách bằng khoảng trắng trong shell đi trong các yếu tố danh sách riêng biệt, trong khi các đối số cần trích dẫn hoặc rút lại khi sử dụng Tên tệp chứa khoảng trắng hoặc lệnh Echo được hiển thị ở trên) là các yếu tố danh sách đơn.

Trên Windows, nếu Args là một chuỗi, nó sẽ được chuyển đổi thành một chuỗi theo cách được mô tả trong việc chuyển đổi một chuỗi đối số thành một chuỗi trên Windows. Điều này là do

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
30 cơ bản hoạt động trên các chuỗi.Converting an argument sequence to a string on Windows. This is because the underlying
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
30 operates on strings.

Đã thay đổi trong phiên bản 3.6: Tham số ARGS chấp nhận một đối tượng giống như đường dẫn nếu shell là

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
45 và một chuỗi chứa các đối tượng giống như đường dẫn trên POSIX.args parameter accepts a path-like object if shell is
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
45 and a sequence containing path-like objects on POSIX.

Thay đổi trong phiên bản 3.8: Tham số Args chấp nhận một đối tượng giống như đường dẫn nếu shell là

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
45 và một chuỗi chứa byte và các đối tượng giống như đường dẫn trên Windows.args parameter accepts a path-like object if shell is
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
45 and a sequence containing bytes and path-like objects on Windows.

Đối số shell (mặc định là

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
45) chỉ định xem có nên sử dụng shell làm chương trình để thực thi hay không. Nếu shell là
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00, nên chuyển Args dưới dạng chuỗi chứ không phải là một chuỗi.

Trên POSIX với

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49, shell mặc định là
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
50. Nếu args là một chuỗi, chuỗi chỉ định lệnh để thực thi thông qua shell. Điều này có nghĩa là chuỗi phải được định dạng chính xác như khi được gõ tại dấu nhắc shell. Điều này bao gồm, ví dụ, trích dẫn hoặc chao đảo thoát khỏi các tên tệp với không gian trong đó. Nếu Args là một chuỗi, mục đầu tiên chỉ định chuỗi lệnh và bất kỳ mục bổ sung nào sẽ được coi là đối số bổ sung cho chính shell. Điều đó có nghĩa là,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 có tương đương với:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

Trên Windows với

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49, biến môi trường
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
53 chỉ định shell mặc định. Lần duy nhất bạn cần chỉ định
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49 trên Windows là khi lệnh bạn muốn thực thi được tích hợp vào shell (ví dụ: dir hoặc sao chép). Bạn không cần
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49 để chạy tệp hàng loạt hoặc thực thi dựa trên bảng điều khiển.
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
53 environment variable specifies the default shell. The only time you need to specify
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49 on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49 to run a batch file or console-based executable.

Bufsize sẽ được cung cấp dưới dạng đối số tương ứng cho hàm

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
56 khi tạo các đối tượng tệp ống stdin/stdout/stderr:

  • import sys
    from sense_hat import SenseHat
    sense = SenseHat()
    sense.clear()
    sense.show_message(sys.argv[1])
    
    57 có nghĩa là không bị ảnh hưởng (đọc và viết là một cuộc gọi hệ thống và có thể quay lại ngắn)

  • import sys
    from sense_hat import SenseHat
    sense = SenseHat()
    sense.clear()
    sense.show_message(sys.argv[1])
    
    58 có nghĩa là bộ đệm dòng (chỉ có thể sử dụng nếu
    import sys
    from sense_hat import SenseHat
    sense = SenseHat()
    sense.clear()
    sense.show_message(sys.argv[1])
    
    59 tức là, ở chế độ văn bản)

  • Bất kỳ giá trị dương nào khác có nghĩa là sử dụng bộ đệm có kích thước xấp xỉ

  • Bufsize âm (mặc định) có nghĩa là mặc định hệ thống của io.default_buffer_size sẽ được sử dụng.

Đã thay đổi trong phiên bản 3.3.1: Bufsize hiện có mặc định thành -1 để bật bộ đệm theo mặc định để phù hợp với hành vi mà hầu hết mã mong đợi. Trong các phiên bản trước Python 3.2.4 và 3.3.1, nó được mặc định không chính xác là

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
57 được không bị truy cập và cho phép đọc ngắn. Điều này là vô ý và không phù hợp với hành vi của Python 2 như hầu hết các mã mong đợi.bufsize now defaults to -1 to enable buffering by default to match the behavior that most code expects. In versions prior to Python 3.2.4 and 3.3.1 it incorrectly defaulted to
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
57 which was unbuffered and allowed short reads. This was unintentional and did not match the behavior of Python 2 as most code expected.

Đối số thực thi chỉ định một chương trình thay thế để thực thi. Nó rất hiếm khi cần thiết. Khi

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
41, thực thi có thể thay thế chương trình để thực hiện được chỉ định bởi Args. Tuy nhiên, ARG ban đầu vẫn được chuyển cho chương trình. Hầu hết các chương trình coi chương trình được ARG chỉ định là tên lệnh, sau đó có thể khác với chương trình thực sự được thực hiện. Trên Posix, tên ARGS trở thành tên hiển thị cho các tiện ích có thể thực thi như PS. Nếu
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49, trên POSIX đối số thực thi chỉ định một shell thay thế cho
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
50 mặc định.ps. If
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49, on POSIX the executable argument specifies a replacement shell for the default
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
50.

Thay đổi trong phiên bản 3.6: Tham số thực thi chấp nhận một đối tượng giống như đường dẫn trên POSIX.executable parameter accepts a path-like object on POSIX.

Thay đổi trong phiên bản 3.8: Tham số thực thi chấp nhận một byte và đối tượng giống như đường dẫn trên Windows.executable parameter accepts a bytes and path-like object on Windows.

Stdin, stdout và stderr chỉ định đầu vào tiêu chuẩn của chương trình, đầu ra tiêu chuẩn và tay cầm tệp tiêu chuẩn, tương ứng. Các giá trị hợp lệ là

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
01,
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
02, một bộ mô tả tệp hiện có (một số nguyên dương), một đối tượng tệp hiện có với bộ mô tả tệp hợp lệ và
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2.
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
01 chỉ ra rằng một đường ống mới cho trẻ nên được tạo ra.
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
02 chỉ ra rằng tệp đặc biệt
Popen(['/bin/sh', '-c', args[0], args[1], ...])
9 sẽ được sử dụng. Với các cài đặt mặc định của
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2, sẽ không có sự chuyển hướng nào xảy ra; Tay cầm tập tin trẻ con sẽ được kế thừa từ cha mẹ. Ngoài ra, STDERR có thể là
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
08, điều này chỉ ra rằng dữ liệu STDERR từ các ứng dụng phải được ghi vào cùng một xử lý tệp như đối với stdout.file object with a valid file descriptor, and
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2.
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
01 indicates that a new pipe to the child should be created.
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
02 indicates that the special file
Popen(['/bin/sh', '-c', args[0], args[1], ...])
9 will be used. With the default settings of
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
08, which indicates that the stderr data from the applications should be captured into the same file handle as for stdout.

Nếu preexec_fn được đặt thành một đối tượng có thể gọi được, đối tượng này sẽ được gọi trong quá trình con ngay trước khi đứa trẻ được thực thi. (Chỉ Posix)

Cảnh báo

Tham số PEREXEC_FN không an toàn để sử dụng với sự hiện diện của các luồng trong ứng dụng của bạn. Quá trình trẻ em có thể bế tắc trước khi EXEC được gọi.

Ghi chú

Nếu bạn cần sửa đổi môi trường cho đứa trẻ sử dụng tham số env thay vì thực hiện nó trong preexec_fn. Các tham số start_new_session và process_group sẽ thay thế mã bằng cách sử dụng preexec_fn để gọi

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
72 hoặc
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
73 ở trẻ.

Đã thay đổi trong phiên bản 3.8: Tham số PreExec_FN không còn được hỗ trợ trong các phần phụ. Việc sử dụng tham số trong trình điều khiển phụ tăng

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
74. Hạn chế mới có thể ảnh hưởng đến các ứng dụng được triển khai trong MOD_WSGI, UWSGI và các môi trường nhúng khác.The preexec_fn parameter is no longer supported in subinterpreters. The use of the parameter in a subinterpreter raises
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
74. The new restriction may affect applications that are deployed in mod_wsgi, uWSGI, and other embedded environments.

Nếu Close_FDS là đúng, tất cả các mô tả tệp ngoại trừ

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
57,
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
58 và
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
77 sẽ bị đóng trước khi quá trình con được thực thi. Mặt khác, khi Close_FDS là sai, các mô tả tệp tuân theo cờ di truyền của họ như được mô tả trong kế thừa của các mô tả tệp.Inheritance of File Descriptors.

Trên Windows, nếu Close_FDS là đúng thì sẽ không có tay cầm nào được kế thừa bởi quy trình con trừ khi được thông qua rõ ràng trong phần tử

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
78 của
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
79 hoặc theo chuyển hướng xử lý tiêu chuẩn.

Đã thay đổi trong phiên bản 3.2: Mặc định cho Close_FDS đã được thay đổi từ

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
45 thành những gì được mô tả ở trên.The default for close_fds was changed from
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
45 to what is described above.

Đã thay đổi trong phiên bản 3.7: Trên Windows, mặc định cho Close_FDS đã được thay đổi từ

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
45 thành
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00 khi chuyển hướng tay cầm tiêu chuẩn. Bây giờ, nó có thể đặt Close_fds thành
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00 khi chuyển hướng các tay cầm tiêu chuẩn.On Windows the default for close_fds was changed from
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
45 to
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00 when redirecting the standard handles. It’s now possible to set close_fds to
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00 when redirecting the standard handles.

pass_fds là một chuỗi các mô tả tệp tùy chọn để tiếp tục mở giữa cha mẹ và con cái. Cung cấp bất kỳ lực lượng pass_fds gần

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00. (Chỉ Posix)

Đã thay đổi trong phiên bản 3.2: Tham số pass_fds đã được thêm vào.The pass_fds parameter was added.

Nếu CWD không phải là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2, chức năng sẽ thay đổi thư mục làm việc thành CWD trước khi thực hiện trẻ. CWD có thể là một chuỗi, byte hoặc đối tượng giống như đường dẫn. Trên POSIX, hàm tìm kiếm thực thi (hoặc cho mục đầu tiên trong ARGS) so với CWD nếu đường dẫn thực thi là một đường dẫn tương đối.path-like object. On POSIX, the function looks for executable (or for the first item in args) relative to cwd if the executable path is a relative path.

Thay đổi trong phiên bản 3.6: Tham số CWD chấp nhận một đối tượng giống như đường dẫn trên POSIX.cwd parameter accepts a path-like object on POSIX.

Thay đổi trong phiên bản 3.7: Tham số CWD chấp nhận một đối tượng giống như đường dẫn trên Windows.cwd parameter accepts a path-like object on Windows.

Thay đổi trong phiên bản 3.8: Tham số CWD chấp nhận đối tượng byte trên Windows.cwd parameter accepts a bytes object on Windows.

Nếu restore_signals là đúng (mặc định), tất cả các tín hiệu mà python đã đặt thành sig_ign được khôi phục thành sig_dfl trong quy trình con trước khi thực hiện. Hiện tại điều này bao gồm các tín hiệu Sigpipe, SigXFZ và SigXFSZ. (Chỉ Posix)

Đã thay đổi trong phiên bản 3.2: Restore_signals đã được thêm vào.restore_signals was added.

Nếu start_new_session là đúng thì cuộc gọi hệ thống

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
86 sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.

Tính khả dụng: Posix: POSIX

Đã thay đổi trong phiên bản 3.2: start_new_session đã được thêm vào.start_new_session was added.

Nếu Process_group là một số nguyên không âm, cuộc gọi hệ thống

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
87 sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.

Tính khả dụng: Posix: POSIX

Đã thay đổi trong phiên bản 3.2: start_new_session đã được thêm vào.process_group was added.

Nếu Process_group là một số nguyên không âm, cuộc gọi hệ thống

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
87 sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.

Tính khả dụng: Posix: POSIX

Đã thay đổi trong phiên bản 3.2: start_new_session đã được thêm vào.

Nếu Process_group là một số nguyên không âm, cuộc gọi hệ thống

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
87 sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.

Tính khả dụng: Posix: POSIX

Đã thay đổi trong phiên bản 3.2: start_new_session đã được thêm vào.

Nếu Process_group là một số nguyên không âm, cuộc gọi hệ thống

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
87 sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.

Tính khả dụng: Posix: POSIX

Đã thay đổi trong phiên bản 3.2: start_new_session đã được thêm vào.

Nếu Process_group là một số nguyên không âm, cuộc gọi hệ thống

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
87 sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.

Tính khả dụng: Posix: POSIX

Đã thay đổi trong phiên bản 3.2: start_new_session đã được thêm vào.

Nếu Process_group là một số nguyên không âm, cuộc gọi hệ thống

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
87 sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.

Đã thay đổi trong phiên bản 3.11: Process_group đã được thêm vào.

Nếu nhóm không phải là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2, cuộc gọi hệ thống setregid () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Nếu giá trị được cung cấp là một chuỗi, nó sẽ được tra cứu qua
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
89 và giá trị trong
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
90 sẽ được sử dụng. Nếu giá trị là một số nguyên, nó sẽ được truyền nguyên văn. (Chỉ Posix)must include a valid
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
00.

Mới trong phiên bản 3.9.Frequently Used Arguments. The universal_newlines argument is equivalent to text and is provided for backwards compatibility. By default, file objects are opened in binary mode.

Nếu Extra_groups không phải là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2, cuộc gọi hệ thống setgroup () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Các chuỗi được cung cấp trong các nhóm thêm sẽ được tra cứu qua
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
89 và các giá trị trong
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
90 sẽ được sử dụng. Giá trị số nguyên sẽ được truyền nguyên văn. (Chỉ Posix)encoding and errors were added.

Nếu người dùng không phải là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2, cuộc gọi hệ thống setreuid () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Nếu giá trị được cung cấp là một chuỗi, nó sẽ được tra cứu qua
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
95 và giá trị trong
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
96 sẽ được sử dụng. Nếu giá trị là một số nguyên, nó sẽ được truyền nguyên văn. (Chỉ Posix)text was added as a more readable alias for universal_newlines.

Nếu UMASK không âm, cuộc gọi hệ thống Umask () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    03

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    04

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    05

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    06

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    07

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    08

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    09

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    10

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    11

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    12

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    13

  • while wait:
                user_position = input("Where do you wanna go? (0, 1, 2, back): ")
                wait = False
                #  Console output
                print("Position: " + str(user_position))
    
                if user_position == "0":
                    stepper.set_target_position(position_zero)
                    wait = True
                elif user_position == "1":
                    stepper.set_target_position(position_one
                    wait = True
                elif user_position == "2":
                    stepper.set_target_position(position_two)
                    wait = True
                elif user_position == "back":
                    break
    
    14

Các đường ống có thể được sử dụng để thay đổi kích thước của đường ống khi

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
01 được sử dụng cho stdin, stdout hoặc stderr. Kích thước của đường ống chỉ được thay đổi trên các nền tảng hỗ trợ điều này (chỉ Linux tại thời điểm viết này). Các nền tảng khác sẽ bỏ qua tham số này.

Mới trong phiên bản 3.10: Tham số

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
16 đã được thêm vào.The
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
16 parameter was added.

Các đối tượng popen được hỗ trợ dưới dạng các trình quản lý ngữ cảnh thông qua câu lệnh

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
17: Khi thoát, các mô tả tệp tiêu chuẩn được đóng và quá trình được chờ đợi.

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())

Popen và các chức năng khác trong mô -đun này sử dụng nó nâng cao sự kiện kiểm toán

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
18 với các đối số
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
19,
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
20,
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
21 và
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
22. Giá trị cho
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
20 có thể là một chuỗi hoặc một danh sách các chuỗi, tùy thuộc vào nền tảng.auditing event
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
18 with arguments
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
19,
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
20,
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
21, and
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
22. The value for
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
20 may be a single string or a list of strings, depending on platform.

Đã thay đổi trong phiên bản 3.2: Đã thêm hỗ trợ Trình quản lý bối cảnh.Added context manager support.

Đã thay đổi trong phiên bản 3.6: Công cụ phá hủy popen hiện đang phát ra cảnh báo

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
24 nếu quá trình con vẫn đang chạy.Popen destructor now emits a
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
24 warning if the child process is still running.

Đã thay đổi trong phiên bản 3.8: Popen có thể sử dụng

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
25 trong một số trường hợp để có hiệu suất tốt hơn. Trên hệ thống con Windows cho mô phỏng người dùng Linux và QEMU, hàm tạo popen sử dụng
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
25 không còn gây ra ngoại lệ về các lỗi như bị thiếu chương trình, nhưng quá trình con không thành công với một
Popen(['/bin/sh', '-c', args[0], args[1], ...])
6 khác không.Popen can use
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
25 in some cases for better performance. On Windows Subsystem for Linux and QEMU User Emulation, Popen constructor using
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
25 no longer raise an exception on errors like missing program, but the child process fails with a non-zero
Popen(['/bin/sh', '-c', args[0], args[1], ...])
6.

Ngoại lệ ha

Các trường hợp ngoại lệ được nêu ra trong quy trình trẻ em, trước khi chương trình mới bắt đầu thực thi, sẽ được nuôi dưỡng lại trong cha mẹ.

Ngoại lệ phổ biến nhất được nâng lên là

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
28. Điều này xảy ra, ví dụ, khi cố gắng thực thi một tệp không tồn tại. Các ứng dụng nên chuẩn bị cho các ngoại lệ
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
28. Lưu ý rằng, khi
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49,
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
28 sẽ được con nuôi chỉ khi không tìm thấy vỏ được chọn. Để xác định xem shell không tìm thấy ứng dụng được yêu cầu, cần phải kiểm tra mã trả về hoặc đầu ra từ quy trình con.

A

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
32 sẽ được nâng lên nếu
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 được gọi với các đối số không hợp lệ.

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
9 và
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
5 sẽ tăng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 nếu quá trình được gọi là trả lại mã trả lại khác không.

Tất cả các chức năng và phương thức chấp nhận tham số thời gian chờ, chẳng hạn như

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
37 và
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 sẽ tăng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 nếu thời gian chờ hết hạn trước khi quá trình thoát ra.

Các ngoại lệ được xác định trong mô -đun này tất cả kế thừa từ

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
3.

Cân nhắc về Bảo mật¶

Không giống như một số chức năng popen khác, việc triển khai này sẽ không bao giờ hoàn toàn gọi một shell hệ thống. Điều này có nghĩa là tất cả các nhân vật, bao gồm cả các metacharacters shell, có thể được truyền một cách an toàn cho các quá trình trẻ em. Nếu vỏ được gọi rõ ràng, thông qua

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49, thì trách nhiệm của ứng dụng là đảm bảo rằng tất cả các chất trắng và metacharacters được trích dẫn một cách thích hợp để tránh các lỗ hổng tiêm vỏ. Trên một số nền tảng, có thể sử dụng
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
42 cho việc trốn thoát này.some platforms, it is possible to use
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
42 for this escaping.

Đối tượng Popen

Các thể hiện của lớp

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 có các phương thức sau:

Popen.poll () ¶poll()

Kiểm tra nếu quá trình trẻ em đã chấm dứt. Đặt và trả về thuộc tính

Popen(['/bin/sh', '-c', args[0], args[1], ...])
6. Nếu không, trả về
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2.

Popen.wait (thời gian chờ = không) ¶wait(timeout=None)

Chờ quá trình trẻ em chấm dứt. Đặt và trả về thuộc tính

Popen(['/bin/sh', '-c', args[0], args[1], ...])
6.

Nếu quá trình không chấm dứt sau thời gian chờ giây, hãy tăng ngoại lệ

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6. Nó là an toàn để bắt được ngoại lệ này và thử lại sự chờ đợi.

Ghi chú

Điều này sẽ bế tắc khi sử dụng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
1 hoặc
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
2 và quy trình con tạo ra đủ đầu ra cho một đường ống sao cho nó chặn chờ bộ đệm ống OS chấp nhận thêm dữ liệu. Sử dụng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5 khi sử dụng đường ống để tránh điều đó.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

Popen.Commenticate (input = none, timeout = none) ¶communicate(input=None, timeout=None)

Tương tác với quy trình: Gửi dữ liệu đến Stdin. Đọc dữ liệu từ stdout và stderr, cho đến khi đạt được phần cuối. Đợi quá trình chấm dứt và đặt thuộc tính

Popen(['/bin/sh', '-c', args[0], args[1], ...])
6. Đối số đầu vào tùy chọn phải là dữ liệu được gửi đến quy trình con hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2, nếu không có dữ liệu nào được gửi cho trẻ. Nếu các luồng được mở ở chế độ văn bản, đầu vào phải là một chuỗi. Nếu không, nó phải là byte.

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
53 Trả về một tuple
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
54. Dữ liệu sẽ là chuỗi nếu các luồng được mở ở chế độ văn bản; Nếu không, byte.

Lưu ý rằng nếu bạn muốn gửi dữ liệu đến quy trình Stdin Stdin, bạn cần tạo đối tượng Popen với

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
9. Tương tự, để có được bất cứ thứ gì khác ngoài
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 trong kết quả, bạn cũng cần cung cấp cho
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
1 và/hoặc
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
2.

Nếu quá trình không chấm dứt sau khi hết thời gian, một ngoại lệ

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6 sẽ được nâng lên. Bắt được ngoại lệ này và giao tiếp thử lại sẽ không mất bất kỳ đầu ra nào.

Quá trình trẻ em không bị giết nếu hết thời gian chờ, vì vậy để dọn dẹp đúng một ứng dụng được cư xử tốt nên tiêu diệt quy trình trẻ em và kết thúc giao tiếp:

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()

Ghi chú

Dữ liệu đọc được đệm trong bộ nhớ, vì vậy không sử dụng phương pháp này nếu kích thước dữ liệu lớn hoặc không giới hạn.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

Popen.send_signal (tín hiệu) ¶send_signal(signal)

Gửi tín hiệu tín hiệu cho trẻ.

Không làm gì nếu quá trình hoàn thành.

Ghi chú

Trên Windows, Sigterm là bí danh cho

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
60. CTRL_C_EVENT và CTRL_BREAK_EVENT có thể được gửi đến các quy trình bắt đầu với tham số CreationFlags bao gồm
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
04.

Popen.erminate () ¶terminate()

Ngừng đứa trẻ. Trên Posix OSS, phương pháp gửi Sigterm cho đứa trẻ. Trên Windows, hàm API Win32

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
62 được gọi để ngăn chặn đứa trẻ.

Popen.kill ()kill()

Giết chết đứa trẻ. Trên posix oss, chức năng gửi sigkill cho trẻ. Trên Windows

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
63 là bí danh cho
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
60.

Các thuộc tính sau cũng có sẵn:

Popen.args¶args

Đối số Args khi nó được chuyển đến

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 - một chuỗi các đối số chương trình hoặc một chuỗi khác.

Mới trong phiên bản 3.3.

Popen.stdin¶stdin

Nếu đối số Stdin là

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
01, thuộc tính này là một đối tượng luồng có thể ghi được được trả về bởi
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
56. Nếu các đối số mã hóa hoặc lỗi được chỉ định hoặc đối số Universal_Newlines là
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00, luồng là luồng văn bản, nếu không thì đó là luồng byte. Nếu đối số Stdin không phải là
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
01, thuộc tính này là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2.

Popen.stdout¶stdout

Nếu đối số stdout là

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
01, thuộc tính này là một đối tượng luồng có thể đọc được được trả về bởi
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
56. Đọc từ luồng cung cấp đầu ra từ quá trình trẻ em. Nếu các đối số mã hóa hoặc lỗi được chỉ định hoặc đối số Universal_Newlines là
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00, luồng là luồng văn bản, nếu không thì đó là luồng byte. Nếu đối số stdout không phải là
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
01, thuộc tính này là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2.

Popen.stderr¶stderr

Nếu đối số STDERR là

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
01, thuộc tính này là một đối tượng luồng có thể đọc được được trả về bởi
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
56. Đọc từ luồng cung cấp đầu ra lỗi từ quá trình con. Nếu các đối số mã hóa hoặc lỗi được chỉ định hoặc đối số Universal_Newlines là
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00, luồng là luồng văn bản, nếu không thì đó là luồng byte. Nếu đối số Stderr không phải là
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
01, thuộc tính này là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2.

Popen.pid¶pid

ID quy trình của quá trình trẻ em.

Lưu ý rằng nếu bạn đặt đối số shell thành

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00, đây là ID quy trình của vỏ được sinh ra.

Popen.returncode¶returncode

Mã trả về con, được đặt bởi

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
82 và
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
83 (và gián tiếp bởi
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
53). Giá trị
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 chỉ ra rằng quá trình này đã chấm dứt.

Giá trị âm

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
7 chỉ ra rằng đứa trẻ bị chấm dứt bởi tín hiệu
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 (chỉ POSIX).

Windows Popen Helpers¶

Lớp

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
01 và các hằng số sau chỉ có sẵn trên Windows.

classSubProcess.StartUpInfo (*, dwflags = 0, hstdinput = none, hstDoutput = none, hstderror = none, wshowWindow = 0, lpattributelist = none)subprocess.STARTUPINFO(*, dwFlags=0, hStdInput=None, hStdOutput=None, hStdError=None, wShowWindow=0, lpAttributeList=None)

Hỗ trợ một phần của cấu trúc Windows StartupInfo được sử dụng để tạo

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7. Các thuộc tính sau đây có thể được đặt bằng cách truyền chúng dưới dạng đối số chỉ từ khóa.

Thay đổi trong phiên bản 3.7: Hỗ trợ đối số chỉ từ khóa đã được thêm vào.Keyword-only argument support was added.

dwflags¶

Một trường bit xác định xem một số thuộc tính

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
01 có được sử dụng khi quá trình tạo ra một cửa sổ.

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW

hstdinput¶

Nếu

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
91 chỉ định
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
92, thuộc tính này là tay cầm đầu vào tiêu chuẩn cho quy trình. Nếu
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
92 không được chỉ định, mặc định cho đầu vào tiêu chuẩn là bộ đệm bàn phím.

Hstdoutput¶

Nếu

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
91 chỉ định
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
92, thuộc tính này là tay cầm đầu ra tiêu chuẩn cho quy trình. Mặt khác, thuộc tính này bị bỏ qua và mặc định cho đầu ra tiêu chuẩn là bộ đệm cửa sổ console.

hstderror¶

Nếu

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
91 chỉ định
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
92, thuộc tính này là xử lý lỗi tiêu chuẩn cho quy trình. Mặt khác, thuộc tính này bị bỏ qua và mặc định cho lỗi tiêu chuẩn là bộ đệm cửa sổ console.

wshowwindow¶

Nếu

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
91 chỉ định
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
99, thuộc tính này có thể là bất kỳ giá trị nào có thể được chỉ định trong tham số
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
00 cho hàm showWindow, ngoại trừ
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
01. Nếu không, thuộc tính này bị bỏ qua.

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
02 được cung cấp cho thuộc tính này. Nó được sử dụng khi
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 được gọi với
import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
49.

lpattributelist¶

Một từ điển các thuộc tính bổ sung để tạo quy trình như được đưa ra trong

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
05, xem UpdateProcThreadAttribution.

Các thuộc tính được hỗ trợ:

handle_list

Trình tự của tay cầm sẽ được kế thừa. Close_fds phải đúng nếu không trống.

Các tay cầm phải tạm thời được kế thừa bằng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
06 khi được chuyển cho hàm tạo
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7, nếu không
while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
28 sẽ được nâng lên với lỗi Windows
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
09 (87).

Cảnh báo

Trong một quy trình đa luồng, sử dụng thận trọng để tránh các tay cầm bị rò rỉ có thể di truyền khi kết hợp tính năng này với các cuộc gọi đồng thời đến các chức năng tạo quy trình khác kế thừa tất cả các tay cầm như

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
10. Điều này cũng áp dụng cho chuyển hướng xử lý tiêu chuẩn, tạm thời tạo ra tay cầm có thể thừa kế.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng số

Mô -đun

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
1 hiển thị các hằng số sau.

SubProcess.std_input_handle¶STD_INPUT_HANDLE

Thiết bị đầu vào tiêu chuẩn. Ban đầu, đây là bộ đệm đầu vào bảng điều khiển,

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
12.

Subprocess.std_output_handle¶STD_OUTPUT_HANDLE

Thiết bị đầu ra tiêu chuẩn. Ban đầu, đây là bộ đệm màn hình bảng điều khiển hoạt động,

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
13.

SubProcess.std_error_handle¶STD_ERROR_HANDLE

Thiết bị lỗi tiêu chuẩn. Ban đầu, đây là bộ đệm màn hình bảng điều khiển hoạt động,

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
13.

quá trình phụ.sw_hide¶SW_HIDE

Giấu cửa sổ. Một cửa sổ khác sẽ được kích hoạt.

phụSTARTF_USESTDHANDLES

Chỉ định rằng các thuộc tính

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
15,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
16 và
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
17 chứa thông tin bổ sung.

SubProcess.startf_useshowwindow¶STARTF_USESHOWWINDOW

Chỉ định rằng thuộc tính

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
18 chứa thông tin bổ sung.

SubProcess.Create_New_Console¶CREATE_NEW_CONSOLE

Quá trình mới có một giao diện điều khiển mới, thay vì kế thừa bảng điều khiển cha mẹ của nó (mặc định).

SubProcess.Create_New_Process_group¶CREATE_NEW_PROCESS_GROUP

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một nhóm quy trình mới sẽ được tạo. Cờ này là cần thiết để sử dụng
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
21 trên quy trình con.

Cờ này bị bỏ qua nếu

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
03 được chỉ định.

phụABOVE_NORMAL_PRIORITY_CLASS

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên trên trung bình.

Mới trong phiên bản 3.7.

phụBELOW_NORMAL_PRIORITY_CLASS

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên dưới mức trung bình.

Mới trong phiên bản 3.7.

phụHIGH_PRIORITY_CLASS

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên cao.

Mới trong phiên bản 3.7.

phụIDLE_PRIORITY_CLASS

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên (thấp nhất).

Mới trong phiên bản 3.7.

phụNORMAL_PRIORITY_CLASS

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên bình thường. (mặc định)

Mới trong phiên bản 3.7.

phụREALTIME_PRIORITY_CLASS

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên thời gian thực. Bạn hầu như không bao giờ nên sử dụng realtime_priority_class, bởi vì điều này làm gián đoạn các luồng hệ thống quản lý đầu vào chuột, đầu vào bàn phím và xả đĩa nền. Lớp này có thể phù hợp với các ứng dụng mà nói chuyện trực tiếp với phần cứng hoặc thực hiện các tác vụ ngắn gọn nên bị gián đoạn hạn chế.

Mới trong phiên bản 3.7.

SubProcess.Create_NO_Window¶CREATE_NO_WINDOW

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một quy trình mới sẽ không tạo cửa sổ.

Mới trong phiên bản 3.7.

quá trình phụ .detached_process¶DETACHED_PROCESS

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một quy trình mới sẽ không kế thừa bảng điều khiển cha mẹ của nó. Giá trị này không thể được sử dụng với create_new_console.

Mới trong phiên bản 3.7.

SubProcess.Create_Default_error_Mode¶CREATE_DEFAULT_ERROR_MODE

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một quy trình mới không kế thừa chế độ lỗi của quá trình gọi. Thay vào đó, quy trình mới có chế độ lỗi mặc định. Tính năng này đặc biệt hữu ích cho các ứng dụng shell đa luồng chạy với các lỗi cứng bị vô hiệu hóa.

Mới trong phiên bản 3.7.

phụCREATE_BREAKAWAY_FROM_JOB

Tham số

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
20 để chỉ định rằng một quy trình mới không liên quan đến công việc.

Mới trong phiên bản 3.7.

API cấp cao cũ hơn

Trước Python 3.5, ba chức năng này bao gồm API cấp cao để xử lý phụ. Bây giờ bạn có thể sử dụng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 trong nhiều trường hợp, nhưng rất nhiều mã hiện tại gọi các chức năng này.

Subprocess.call (args, *, stdin = none, stdout = none, stderr = none, shell = falsecall(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

Chạy lệnh được mô tả bởi Args. Đợi lệnh hoàn thành, sau đó trả lại thuộc tính

Popen(['/bin/sh', '-c', args[0], args[1], ...])
6.

Mã cần bắt giữ Stdout hoặc Stderr nên sử dụng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 thay thế:

Để ngăn chặn stdout hoặc stderr, cung cấp giá trị

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
02.

Các đối số hiển thị ở trên chỉ là một số thông thường. Chữ ký chức năng đầy đủ giống như hàm xây dựng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 - hàm này chuyển tất cả các đối số được cung cấp khác ngoài thời gian chờ trực tiếp đến giao diện đó.

Ghi chú

Không sử dụng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
1 hoặc
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
2 với chức năng này. Quá trình con sẽ chặn nếu nó tạo ra đủ đầu ra cho một đường ống để lấp đầy bộ đệm ống OS vì các đường ống không được đọc từ đó.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

Subprocess.check_call (args, *, stdin = none, stdout = none, stderr = none, shell = falsecheck_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

Chạy lệnh với các đối số. Đợi lệnh hoàn thành. Nếu mã trả về bằng không thì hãy quay lại, nếu không thì tăng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0. Đối tượng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 sẽ có mã trả về trong thuộc tính
Popen(['/bin/sh', '-c', args[0], args[1], ...])
6. Nếu
proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
9 không thể bắt đầu quá trình, nó sẽ tuyên truyền ngoại lệ đã được nêu ra.

Mã cần bắt giữ Stdout hoặc Stderr nên sử dụng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 thay thế:

Để ngăn chặn stdout hoặc stderr, cung cấp giá trị

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
02.

Các đối số hiển thị ở trên chỉ là một số thông thường. Chữ ký chức năng đầy đủ giống như hàm xây dựng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7 - hàm này chuyển tất cả các đối số được cung cấp khác ngoài thời gian chờ trực tiếp đến giao diện đó.

Ghi chú

Không sử dụng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
1 hoặc
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
2 với chức năng này. Quá trình con sẽ chặn nếu nó tạo ra đủ đầu ra cho một đường ống để lấp đầy bộ đệm ống OS vì các đường ống không được đọc từ đó.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

Subprocess.check_output (args, *, stdin = none, stderr = none, shell = false, cwd = none, encoding = nonecheck_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, errors=None, universal_newlines=None, timeout=None, text=None, **other_popen_kwargs)

Chạy lệnh với các đối số và trả về đầu ra của nó.

Nếu mã trả lại không khác, nó sẽ tăng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0. Đối tượng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 sẽ có mã trả về trong thuộc tính
Popen(['/bin/sh', '-c', args[0], args[1], ...])
6 và bất kỳ đầu ra nào trong thuộc tính
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
62.

Điều này tương đương với:

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
0

Các đối số hiển thị ở trên chỉ là một số thông thường. Chữ ký chức năng đầy đủ phần lớn giống như

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 - hầu hết các đối số được chuyển trực tiếp qua giao diện đó. Tồn tại một độ lệch API so với hành vi
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6: vượt qua
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
65 sẽ hoạt động giống như
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
66 (hoặc
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
67, tùy thuộc vào các đối số khác) thay vì sử dụng xử lý tệp đầu vào tiêu chuẩn của cha mẹ.

Theo mặc định, hàm này sẽ trả về dữ liệu dưới dạng byte được mã hóa. Mã hóa thực tế của dữ liệu đầu ra có thể phụ thuộc vào lệnh được gọi, do đó việc giải mã thành văn bản thường sẽ cần được xử lý ở cấp độ ứng dụng.

Hành vi này có thể bị ghi đè bằng cách đặt văn bản, mã hóa, lỗi hoặc Universal_Newlines thành

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
00 như được mô tả trong các đối số được sử dụng thường xuyên và
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6.Frequently Used Arguments and
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6.

Để nắm bắt lỗi tiêu chuẩn trong kết quả, hãy sử dụng

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1:

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
1

Mới trong phiên bản 3.1.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

Subprocess.check_output (args, *, stdin = none, stderr = none, shell = false, cwd = none, encoding = noneSupport for the input keyword argument was added.

Chạy lệnh với các đối số và trả về đầu ra của nó.encoding and errors were added. See

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 for details.

Nếu mã trả lại không khác, nó sẽ tăng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0. Đối tượng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0 sẽ có mã trả về trong thuộc tính
Popen(['/bin/sh', '-c', args[0], args[1], ...])
6 và bất kỳ đầu ra nào trong thuộc tính
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
62.text was added as a more readable alias for universal_newlines.

Điều này tương đương với:

Các đối số hiển thị ở trên chỉ là một số thông thường. Chữ ký chức năng đầy đủ phần lớn giống như

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 - hầu hết các đối số được chuyển trực tiếp qua giao diện đó. Tồn tại một độ lệch API so với hành vi
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6: vượt qua
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
65 sẽ hoạt động giống như
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
66 (hoặc
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
67, tùy thuộc vào các đối số khác) thay vì sử dụng xử lý tệp đầu vào tiêu chuẩn của cha mẹ.

Ghi chú

Không sử dụng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
1 hoặc
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
2 với chức năng này. Quá trình con sẽ chặn nếu nó tạo ra đủ đầu ra cho một đường ống để lấp đầy bộ đệm ống OS vì các đường ống không được đọc từ đó.

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.

Subprocess.check_output (args, *, stdin = none, stderr = none, shell = false, cwd = none, encoding = none

Chạy lệnh với các đối số và trả về đầu ra của nó./bin/sh shell command substitution¶

becomes:

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
2

Nếu mã trả lại không khác, nó sẽ tăng >>> import shlex, subprocess >>> command_line = input() /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'" >>> args = shlex.split(command_line) >>> print(args) ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"] >>> p = subprocess.Popen(args) # Success! 0. Đối tượng >>> import shlex, subprocess >>> command_line = input() /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'" >>> args = shlex.split(command_line) >>> print(args) ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"] >>> p = subprocess.Popen(args) # Success! 0 sẽ có mã trả về trong thuộc tính Popen(['/bin/sh', '-c', args[0], args[1], ...]) 6 và bất kỳ đầu ra nào trong thuộc tính >>> subprocess.run(["ls", "-l"]) # doesn't capture output CompletedProcess(args=['ls', '-l'], returncode=0) >>> subprocess.run("exit 1", shell=True, check=True) Traceback (most recent call last): ... subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1 >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True) CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0, stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'') 62.

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
3

becomes:

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
4

Điều này tương đương với:

Các đối số hiển thị ở trên chỉ là một số thông thường. Chữ ký chức năng đầy đủ phần lớn giống như

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6 - hầu hết các đối số được chuyển trực tiếp qua giao diện đó. Tồn tại một độ lệch API so với hành vi
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6: vượt qua
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
65 sẽ hoạt động giống như
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
66 (hoặc
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
67, tùy thuộc vào các đối số khác) thay vì sử dụng xử lý tệp đầu vào tiêu chuẩn của cha mẹ.

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
3

becomes:

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
6

Theo mặc định, hàm này sẽ trả về dữ liệu dưới dạng byte được mã hóa. Mã hóa thực tế của dữ liệu đầu ra có thể phụ thuộc vào lệnh được gọi, do đó việc giải mã thành văn bản thường sẽ cần được xử lý ở cấp độ ứng dụng.

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
7

Notes:

  • Hành vi này có thể bị ghi đè bằng cách đặt văn bản, mã hóa, lỗi hoặc Universal_Newlines thành

    import sys
    from sense_hat import SenseHat
    sense = SenseHat()
    sense.clear()
    sense.show_message(sys.argv[1])
    
    00 như được mô tả trong các đối số được sử dụng thường xuyên và
    >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    6.

  • Để nắm bắt lỗi tiêu chuẩn trong kết quả, hãy sử dụng

    Popen(['/bin/sh', '-c', args[0], args[1], ...])
    
    1:

  • Mới trong phiên bản 3.1.

Thay đổi trong phiên bản 3.4: Hỗ trợ cho đối số từ khóa đầu vào đã được thêm vào.

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
8

Thay đổi trong phiên bản 3.6: Mã hóa và lỗi đã được thêm vào. Xem >>> subprocess.run(["ls", "-l"]) # doesn't capture output CompletedProcess(args=['ls', '-l'], returncode=0) >>> subprocess.run("exit 1", shell=True, check=True) Traceback (most recent call last): ... subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1 >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True) CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0, stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'') 6 để biết chi tiết.

Mới trong phiên bản 3.7: Văn bản đã được thêm vào dưới dạng bí danh dễ đọc hơn cho Universal_Newlines.

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])
9

Thay thế các chức năng cũ bằng mô -đun

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
1

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
0

Trong phần này, A A trở thành B Bi có nghĩa là B có thể được sử dụng thay thế cho a.

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
1

Tất cả các chức năng của một người khác trong phần này đều không thành công (ít nhiều) nếu không thể tìm thấy chương trình thực hiện; Thay vào đó, các thay thế của B Biêu tăng

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
28 thay thế.

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
2

Ngoài ra, việc thay thế sử dụng with Popen(["ifconfig"], stdout=PIPE) as proc: log.write(proc.stdout.read()) 5 sẽ thất bại với >>> import shlex, subprocess >>> command_line = input() /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'" >>> args = shlex.split(command_line) >>> print(args) ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"] >>> p = subprocess.Popen(args) # Success! 0 nếu hoạt động được yêu cầu tạo ra mã trả lại khác không. Đầu ra vẫn có sẵn dưới dạng thuộc tính >>> subprocess.run(["ls", "-l"]) # doesn't capture output CompletedProcess(args=['ls', '-l'], returncode=0) >>> subprocess.run("exit 1", shell=True, check=True) Traceback (most recent call last): ... subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1 >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True) CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0, stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'') 62 của ngoại lệ được nâng lên.

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
3

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
4

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
5

Trong các ví dụ sau, chúng tôi giả định rằng các chức năng liên quan đã được nhập từ mô -đun

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
1.

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
6

Thay thế /Bin /SH Shell Lệnh thay thế SHELL

Ghi chú

Không sử dụng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
1 hoặc
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
2 với chức năng này. Quá trình con sẽ chặn nếu nó tạo ra đủ đầu ra cho một đường ống để lấp đầy bộ đệm ống OS vì các đường ống không được đọc từ đó.

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
7

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
8

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    7 làm tăng một ngoại lệ nếu việc thực hiện thất bại.

  • Đối số Capturestderr được thay thế bằng đối số Stderr.

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    9 và
    Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    1 phải được chỉ định.

  • Popen2 đóng tất cả các mô tả tệp theo mặc định, nhưng bạn phải chỉ định

    >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    95 với
    >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    7 để đảm bảo hành vi này trên tất cả các nền tảng hoặc các phiên bản Python trong quá khứ.

Chức năng gọi vỏ di sản Legacy

Mô -đun này cũng cung cấp các hàm di sản sau đây từ mô -đun 2.x

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
97. Các hoạt động này hoàn toàn gọi Shell hệ thống và không có sự đảm bảo nào được mô tả ở trên về tính nhất quán xử lý bảo mật và ngoại lệ là hợp lệ cho các chức năng này.

Subprocess.getStatusOutput (cmd, *, mã hóa = không, lỗi = không) ¶getstatusoutput(cmd, *, encoding=None, errors=None)

Trả về

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
98 của việc thực thi CMD trong vỏ.

Thực hiện chuỗi CMD trong shell với

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
99 và trả về 2-Tuple
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
98. Mã hóa và lỗi được sử dụng để giải mã đầu ra; Xem các ghi chú về các đối số thường được sử dụng để biết thêm chi tiết.Frequently Used Arguments for more details.

Một dòng mới được truyền ra từ đầu ra. Mã thoát cho lệnh có thể được hiểu là mã trả lại của quy trình con. Thí dụ:

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break
9

Tính khả dụng: UNIX, Windows.: Unix, Windows.

Thay đổi trong phiên bản 3.3.4: Hỗ trợ Windows đã được thêm vào.Windows support was added.

Hàm bây giờ trả về (outcode, đầu ra) thay vì (trạng thái, đầu ra) như trong Python 3.3.3 và sớm hơn. EXITCODE có cùng giá trị với

Popen(['/bin/sh', '-c', args[0], args[1], ...])
6.

Mới trong phiên bản 3.11: Đã thêm các đối số mã hóa và lỗi.Added encoding and errors arguments.

Subprocess.getOutput (cmd, *, mã hóa = không, lỗi = không) ¶getoutput(cmd, *, encoding=None, errors=None)

Trả về đầu ra (stdout và stderr) của việc thực thi cmd trong shell.

Giống như

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
02, ngoại trừ mã thoát bị bỏ qua và giá trị trả về là một chuỗi chứa đầu ra của lệnh. Thí dụ:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
0

Tính khả dụng: UNIX, Windows.: Unix, Windows.

Thay đổi trong phiên bản 3.3.4: Hỗ trợ Windows đã được thêm vào.Windows support added

Mới trong phiên bản 3.11: Đã thêm các đối số mã hóa và lỗi.Added encoding and errors arguments.

Subprocess.getOutput (cmd, *, mã hóa = không, lỗi = không) ¶

Trả về đầu ra (stdout và stderr) của việc thực thi cmd trong shell.

Giống như

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
02, ngoại trừ mã thoát bị bỏ qua và giá trị trả về là một chuỗi chứa đầu ra của lệnh. Thí dụ:

  1. Đã thay đổi trong phiên bản 3.3.4: được thêm hỗ trợ Windows

  2. Notes¶

  3. Chuyển đổi một chuỗi đối số thành một chuỗi trên Windows¶

  4. Trên Windows, một chuỗi Args được chuyển đổi thành một chuỗi có thể được phân tích cú pháp bằng các quy tắc sau (tương ứng với các quy tắc được sử dụng bởi thời gian chạy MS C):

  5. Các đối số được phân định bởi không gian trắng, đó là không gian hoặc một tab.

Một chuỗi được bao quanh bởi các dấu ngoặc kép được hiểu là một đối số duy nhất, bất kể không gian trắng có trong. Một chuỗi được trích dẫn có thể được nhúng trong một đối số.

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
03

Một dấu ngoặc kép có trước một dấu gạch chéo ngược được hiểu là một dấu ngoặc kép theo nghĩa đen.

Backslashes được giải thích theo nghĩa đen, trừ khi chúng ngay lập tức đi trước dấu ngoặc kép.

Nếu dấu gạch chéo ngược ngay trước dấu ngoặc kép, mỗi cặp dấu gạch chéo ngược được hiểu là một dấu gạch chéo ngược theo nghĩa đen. Nếu số lượng dấu gạch chéo ngược là số lẻ, dấu gạch chéo ngược cuối cùng sẽ thoát khỏi dấu ngoặc kép tiếp theo như được mô tả trong Quy tắc 3.

Xem thêm

Mô -đun cung cấp chức năng cho các dòng lệnh phân tích và thoát.

Vô hiệu hóa việc sử dụng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
04 hoặc ________ 405¶

Trên Linux,

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
1 mặc định sử dụng hệ thống
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
04 gọi nội bộ khi an toàn để làm như vậy thay vì
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
08. Điều này cải thiện đáng kể hiệu suất.

Nếu bạn từng gặp phải một tình huống được cho là rất bất thường khi bạn cần ngăn chặn

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
04 được Python sử dụng, bạn có thể đặt thuộc tính
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
10 thành giá trị sai.

SubProcess._USE_VFORK = FALSE # Xem vấn đề Cpython Gh-Nnnnnn.

Cài đặt Điều này không có tác động đến việc sử dụng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
05 có thể sử dụng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
04 trong nội bộ trong quá trình triển khai LIBC của nó. Có một thuộc tính tương tự ____413 nếu bạn cần ngăn chặn việc sử dụng điều đó.
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
14

SubProcess._USE_POSIX_SPAWN = FALSE # Xem Cpython vấn đề GH-Nnnnnn.

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15