当调用pexpect.spawn时,生成的程序的执行是否立即开始?(When pexpect.spawn is called, does execution of the spawned progra

编程入门 行业动态 更新时间:2024-10-24 10:27:14
当调用pexpect.spawn时,生成的程序的执行是否立即开始?(When pexpect.spawn is called, does execution of the spawned program start immediately?)

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.

更多推荐

本文发布于:2023-07-22 12:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219935.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   called   pexpect   spawn   execution

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!