期望:等到进程终止

时间:2020-02-07 14:18:16

标签: python parent-child pexpect

我正在尝试与pexpect合作,以生成另一个Python程序的子进程。 所需的操作应该花费很长时间(甚至5分钟),因此我希望我的父进程不要杀死子进程,而要等到任务完全完成。 任务完成后,父进程应在标准输出中打印子进程本应打印的所有输出。

child = pexpect.spawn('python3 /home/robb/Workspace/prog1.py')

# [...] some input-output ipc correctly working    

# The final request from prog1:
child.expect(".*phrase:")
child.sendline(answer) # Answer is a string
print("DEBUG 1 ##############################")
print(child.after.decode('ascii'))

# Here prog1 should be working for long
# Its last sentence should be like "Found 15 products"
child.expect(".*products")
print("DEBUG 2 ##############################")
print(child.after.decode('ascii'))
print("Work terminated. Closing child process.")
child.close()

我得到的输出如下:

DEBUG 1 ##############################
 10
Input search phrase:

10是上一个问题的答案,据我所知,它只是被缓冲并发送到我在“ DEBUG 1”打印下写的打印中。但是为什么我没有得到以下任何输出?该程序显然在child.expect(".*products")处停止。有什么问题吗?

编辑:这是我遇到的标准错误

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 111, in expect_loop
    incoming = spawn.read_nonblocking(spawn.maxread, timeout)
File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 482, in read_nonblocking
    raise TIMEOUT('Timeout exceeded.')
pexpect.exceptions.TIMEOUT: Timeout exceeded.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "prog2.py", line 39, in <module>
    child.expect(".*products")
  File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 341, in expect
    timeout, searchwindowsize, async_)
  File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 369, in expect_list
    return exp.expect_loop(timeout)
  File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 119, in expect_loop
    return self.timeout(e)
  File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 82, in timeout
    raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0x7f7a9a745710>
command: /usr/bin/python3
args: ['/usr/bin/python3', '/home/robb/Workspace/prog1.py']
buffer (last 100 chars): b'\n103.220.207.242:54349.....FAILED\r\n41.190.147.158:49088.....PASSED\r\n183.88.213.25:50440.....FAILED\r\n'
before (last 100 chars): b'\n103.220.207.242:54349.....FAILED\r\n41.190.147.158:49088.....PASSED\r\n183.88.213.25:50440.....FAILED\r\n'
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 14257
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: re.compile(b'.*products')

0 个答案:

没有答案
相关问题