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

Python pexpect没有使用pexpect.expect阻止我的脚本,python,shell,pexpect,Python,Shell,Pexpect,我正在用Python的pexpect制作一个任务调度器。 这是通过一个简单的想法实现的: 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_T

我正在用Python的pexpect制作一个任务调度器。
这是通过一个简单的想法实现的:

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在孩子开始工作之前匹配了我的SHELL提示。我在匹配之前增加了一些延迟。但是,即使在pexect.expect之前添加延迟,也会发生这种情况

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

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

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

我通过将支票代码保存在txt文件中解决了这个问题。 孩子可以通过在孩子中调用“cat”来反馈此类文件。
例如:

#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