简体   繁体   中英

pexpect did not block my script with pexpect.expect

I am making a task scheduler with pexpect of Python.
This was implemented with a simple idea:

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)

But I found pexpect.expect did not block this line:

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

Since it works with matching pattern set to the last output of previous task. I suspect the pexpect.expect matched MY_SHELL_PROMPT before the child starts its job. I have add some delay before matching. However, this happens even if I add delay before pexect.expect.

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

Does anyone know how to debug this? Any help would be appreciate.

I think I found the answer myself.
pexpect does not distinguish echoed command and output from child.
So it is difficult to accomplish this with my previous attempt.

I workarounded this with saving my check code in a txt file. Such file could be feedback by child with calling 'cat' in child.
For Example:

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM