gpt4 book ai didi

python - 调用 pexpect.spawn 时,生成的程序会立即开始执行吗?

转载 作者:行者123 更新时间:2023-11-28 16:47:31 25 4
gpt4 key购买 nike

pexpect.spawn 对象是否处于非事件状态,直到 expect(或 interactsend 等)被调用,还是被调用的进程立即开始?例如:

import pexpect
process = pexpect.spawn("echo HELLO")
print ("Process created? Or run?")
process.expect("HELLO")

print 语句出现时,pexpect 已经在幕后运行了 echo 命令,只是推迟让它返回,直到 expect 调用已处理?或者在第一次调用 expect 或类似函数之前什么都没发生(echo 没有运行)?

干杯

最佳答案

当传递命令时,pexpect.spawn() 立即 fork 并调用命令。您的示例中的 echo 命令已经运行。

这隐含在 spawn constructor documentation 中:

This is the constructor. The command parameter may be a string that includes a command and any arguments to the command. For example::

child = pexpect.spawn ('/usr/bin/ftp')
child = pexpect.spawn ('/usr/bin/ssh user@example.com')
child = pexpect.spawn ('ls -latr /tmp')

You may also construct it with a list of arguments like so::

child = pexpect.spawn ('/usr/bin/ftp', [])
child = pexpect.spawn ('/usr/bin/ssh', ['user@example.com'])
child = pexpect.spawn ('ls', ['-latr', '/tmp'])

After this the child application will be created and will be ready to talk to.

我已经通过查看 source code 确认了这一点;构造函数调用 _spawn(),记录如下:

This starts the given command in a child process. This does all the fork/exec type of stuff for a pty. This is called by __init__. If args is empty then command will be parsed (split on spaces) and args will be set to parsed arguments.

关于python - 调用 pexpect.spawn 时,生成的程序会立即开始执行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12303061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com