pexpect没有使用pexpect.expect阻止我的脚本

时间:2015-09-21 06:08:31

标签: python shell pexpect

我正在制作一个与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')

有谁知道如何调试这个?任何帮助都会很感激。

1 个答案:

答案 0 :(得分:0)

我想我自己找到了答案 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