【问题标题】:pexpect did not block my script with pexpect.expectpexpect 没有用 pexpect.expect 阻止我的脚本
【发布时间】:2015-09-21 06:08:31
【问题描述】:

我正在用 Python 编写一个任务调度程序。
这是用一个简单的想法实现的:

term = spawnu('tcsh') # I need a tcsh instead of its default bash
term.sendline('FIRST_TASK')
term.expect('MY_SHELL_PROMPT') # When parent receive prompt means end of previous task.
term.sendline('SECOND_TASK')
...(and so on)

但我发现 pexpect.expect 并没有阻塞这一行:

term.expect('MY_SHELL_PROMPT') # Go through this line before finish of previous task.

因为它适用于设置为上一个任务的最后输出的匹配模式。我怀疑 pexpect.expect 在孩子开始工作之前匹配了 MY_SHELL_PROMPT。我在匹配之前添加了一些延迟。但是,即使我在 pexect.expect 之前添加延迟也会发生这种情况。

time.sleep(2)  # delay for 2 second 
term.expect('MY_SHELL_PROMPT')

有人知道如何调试吗?任何帮助将不胜感激。

【问题讨论】:

    标签: python shell pexpect


    【解决方案1】:

    我想我自己找到了答案。
    pexpect 不区分回显命令和子输出。
    所以我之前的尝试很难做到这一点。

    我通过将检查代码保存在 txt 文件中来解决此问题。 这样的文件可能是孩子的反馈,在孩子中叫“猫”。
    例如:

    #check_code.txt
    ----YOUR JOB IS DONE----
    
    #In testPexpect.py
    term.sendline('cat check_code.txt')  # this prevents matching its echoed command
    term.expect('----YOUR JOB IS DONE----') # blocks and matches successfully
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-08
      • 2017-06-07
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2015-11-12
      相关资源
      最近更新 更多