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

标签 python pexpect

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/

相关文章:

python - 如何找到 Python 中两个日期时间对象之间的毫秒差异?

python - pycurl 与谷歌应用引擎

python - 使用现有列和循环的总和创建列

python - Airflow 中的执行顺序

python - 使用 python pexpect 让多线程处理列表

python - 无法在cygwin中使用python的pexpect

python - 检测相似图像

Python pexpect 脚本运行无误,但输出文件中没有输出

python - 使用 pexpect (python) 时 SFTP 未完成文件传输