pexpect.spawn对象是否处于非活动状态,直到调用expect (或interact , send等),或者被调用的进程是否立即启动? 例如:
import pexpect process = pexpect.spawn("echo HELLO") print ("Process created? Or run?") process.expect("HELLO")当print语句出现时, pexpect已经在封面下运行了echo命令并且只是pexpect让它返回直到处理expect调用? 或者没有发生任何事情( echo没有运行),直到第一次expect或类似的呼叫?
干杯
Are pexpect.spawn objects inactive until expect (or interact, send, etc) is called, or does the invoked process start immediately? For example:
import pexpect process = pexpect.spawn("echo HELLO") print ("Process created? Or run?") process.expect("HELLO")When the print statement occurs, has pexpect already run the echo command under the covers and is just holding off on letting it return until the expect call is processed? Or has nothing happened (echo hasn't run) until the first call to expect or similar?
Cheers
最满意答案
传递命令时, pexpect.spawn()立即调用该命令。 您的示例中的echo命令已经运行。
这是在spawn构造函数文档中暗示的:
这是构造函数。 命令参数可以是包含命令和命令的任何参数的字符串。 例如::
child = pexpect.spawn ('/usr/bin/ftp') child = pexpect.spawn ('/usr/bin/ssh user@example.com') child = pexpect.spawn ('ls -latr /tmp')您也可以使用像这样的参数列表来构造它::
child = pexpect.spawn ('/usr/bin/ftp', []) child = pexpect.spawn ('/usr/bin/ssh', ['user@example.com']) child = pexpect.spawn ('ls', ['-latr', '/tmp'])在此之后,将创建子应用程序并准备好与之交谈。
我通过查看源代码证实了这一点; 构造函数调用_spawn() ,其记录如下:
这将在子进程中启动给定命令。 这为pty做了所有fork / exec类型的东西。 这由__init__调用。 如果args为空,则将解析命令(在空格上拆分),并将args设置为已解析的参数。
When passed a command, pexpect.spawn() forks and invokes the command immediately. The echo command in your example would have been run already.
This is implied in the 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.
I've confirmed this by looking at the source code; the constructor calls _spawn() which is documented as follows:
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.
更多推荐
发布评论