保持子进程处于活动状态并继续向其发出命令? Python

发布于 2025-01-06 19:52:24 字数 124 浏览 0 评论 0原文

如果我使用给定的命令在 python 中生成一个新的 subprocess (假设我使用 python 命令启动 python 解释器),我如何将新数据发送到该进程(通过标准输入)?

If I spawn a new subprocess in python with a given command (let's say I start the python interpreter with the python command), how can I send new data to the process (via STDIN)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平生欢 2025-01-13 19:52:24

使用标准 subprocess 模块。您使用 subprocess.Popen() 启动该进程,它将在后台运行(即与您的 Python 程序同时运行)。当您调用 Popen() 时,您可能希望将 stdin、stdout 和 stderr 参数设置为 subprocess.PIPE。然后,您可以使用返回对象上的 stdin、stdout 和 stderr 字段来写入和读取数据。

未经测试的示例代码:

from subprocess import Popen, PIPE

# Run "cat", which is a simple Linux program that prints it's input.
process = Popen(['/bin/cat'], stdin=PIPE, stdout=PIPE)
process.stdin.write(b'Hello\n')
process.stdin.flush()
print(repr(process.stdout.readline())) # Should print 'Hello\n'
process.stdin.write(b'World\n')
process.stdin.flush()  
print(repr(process.stdout.readline())) # Should print 'World\n'

# "cat" will exit when you close stdin.  (Not all programs do this!)
process.stdin.close()
print('Waiting for cat to exit')
process.wait()
print('cat finished with return code %d' % process.returncode)

Use the standard subprocess module. You use subprocess.Popen() to start the process, and it will run in the background (i.e. at the same time as your Python program). When you call Popen(), you probably want to set the stdin, stdout and stderr parameters to subprocess.PIPE. Then you can use the stdin, stdout and stderr fields on the returned object to write and read data.

Untested example code:

from subprocess import Popen, PIPE

# Run "cat", which is a simple Linux program that prints it's input.
process = Popen(['/bin/cat'], stdin=PIPE, stdout=PIPE)
process.stdin.write(b'Hello\n')
process.stdin.flush()
print(repr(process.stdout.readline())) # Should print 'Hello\n'
process.stdin.write(b'World\n')
process.stdin.flush()  
print(repr(process.stdout.readline())) # Should print 'World\n'

# "cat" will exit when you close stdin.  (Not all programs do this!)
process.stdin.close()
print('Waiting for cat to exit')
process.wait()
print('cat finished with return code %d' % process.returncode)
流殇 2025-01-13 19:52:24

不。

如果要将命令发送到子进程,请创建一个 pty,然后分叉该子进程,并将 pty 的一端附加到其 STDIN。

这是我的一些代码的片段:

RNULL = open('/dev/null', 'r')
WNULL = open('/dev/null', 'w')

master, slave = pty.openpty()
print parsedCmd
self.subp = Popen(parsedCmd, shell=False, stdin=RNULL,
                      stdout=WNULL, stderr=slave)

在这段代码中,pty 附加到 stderr,因为它接收错误消息而不是发送命令,但原理是相同的。

Don't.

If you want to send commands to a subprocess, create a pty and then fork the subprocess with one end of the pty attached to its STDIN.

Here is a snippet from some of my code:

RNULL = open('/dev/null', 'r')
WNULL = open('/dev/null', 'w')

master, slave = pty.openpty()
print parsedCmd
self.subp = Popen(parsedCmd, shell=False, stdin=RNULL,
                      stdout=WNULL, stderr=slave)

In this code, the pty is attached to stderr because it receives error messages rather than sending commands, but the principle is the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文