pexpect 超时,来自孩子的大块数据 [英] pexpect timeout with large block of data from child

查看:19
本文介绍了pexpect 超时,来自孩子的大块数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pexpect 调用另一个提示输入 raw_input 的 python 脚本 (py27).我正在尝试围绕此脚本构建一个 GUI 包装器,而无需对其进行修改.

I am using pexpect to call another python script (py27) that prompts for raw_input. I'm trying to build a GUI wrapper around this script without modifying it.

我遇到的问题是我调用的脚本在下一个命令提示符之前执行时返回了大量数据(例如,10KB).我知道 pexpect 会处理大量数据.child.expect 调用总是达到超时,无论我传递什么值.如何处理大数据集?我已经学会了通过手动读取 child.expect.read(X) 来管理它的方法,但这是 hacky.有某种缓冲正在进行,我不知道如何干净地解决它.脚本返回的数据不能扔掉,需要被解析,所以我不能让一个线程总是读取,除非它可以以某种方式写入文件.

The problem I have is that the script I call has a large amount of data returned when executing (e.g., 10KB) before the next command prompt. I am aware that pexpect struggles with large amounts of data. The child.expect call always hits the timeout, no matter what value I pass it. How do I deal with large data sets? I've learned ways to manage this with manual reads child.expect.read(X), but this is hacky. There's some sort of buffering going on and I don't know how to work around it cleanly. The script return data is not to throw away and needs to be parsed so I can't just have a thread always reading, unless it could somehow write to a file.

这是一个例子.我在 Windows 10 上使用 Py2.7

Here is an example. I'm on Windows 10 and using Py2.7

test2.py(可以修改这个脚本,但不能修改test1.py)

test2.py (can modify this script, but not test1.py)

import pexpect
from pexpect import popen_spawn
import time

child = pexpect.popen_spawn.PopenSpawn("python test1.py", maxread=1)
# send hello to child
child.sendline("hello")
# wait for command prompt ">"
child.expect(">", timeout=30)

test1.py(模拟我真实脚本的行为,开始时打印一大块数据,当命令完成时返回>"作为提示,不能修改这个脚本)

test1.py (models behavior of my real script, prints a large block of data at beginning, returns ">" as a prompt when a command completes, can't modify this script)

import sys

# print a bunch of data
for i in range(2000):
    print(i)

try:
    while True:
        # ask for input
        x = raw_input(">")

except KeyboardInterrupt:
    sys.exit("Manually quit")

回溯(最近一次调用最后一次):文件test2.py",第 23 行,在child.expect(">", timeout=10)文件C:************\AppData\Local\conda\conda\envs\py27\lib\site-packages\pexpect\spawnbase.py",第327行,在expect超时、搜索窗口大小、异步_)文件C:************\AppData\Local\conda\conda\envs\py27\lib\site-packages\pexpect\spawnbase.py",第 355 行,在expect_list返回 exp.expect_loop(timeout)文件C:************\AppData\Local\conda\conda\envs\py27\lib\site-packages\pexpect\expect.py",第 106 行,在expect_loop返回 self.timeout(e)文件C:************\AppData\Local\conda\conda\envs\py27\lib\site-packages\pexpect\expect.py",第 70 行,超时提高超时(味精)pexpect.exceptions.TIMEOUT:搜索者: searcher_re:0:重新编译(>")搜索者: searcher_re:0: 重新编译(">")

Traceback (most recent call last): File "test2.py", line 23, in child.expect(">", timeout=10) File "C:************\AppData\Local\conda\conda\envs\py27\lib\site-packages\pexpect\spawnbase.py", line 327, in expect timeout, searchwindowsize, async_) File "C:************\AppData\Local\conda\conda\envs\py27\lib\site-packages\pexpect\spawnbase.py", line 355, in expect_list return exp.expect_loop(timeout) File "C:************\AppData\Local\conda\conda\envs\py27\lib\site-packages\pexpect\expect.py", line 106, in expect_loop return self.timeout(e) File "C:************\AppData\Local\conda\conda\envs\py27\lib\site-packages\pexpect\expect.py", line 70, in timeout raise TIMEOUT(msg) pexpect.exceptions.TIMEOUT: searcher: searcher_re: 0: re.compile(">") searcher: searcher_re: 0: re.compile(">")

推荐答案

如果您确定提示肯定会出现并且您的场景可以永远等待,那么在 expect 中使用 timeout=None 将无限期地等待提示出现.但是,是的,无限期地等待有其自身的负面后果.我建议只有在 100% 确定会出现提示并且无限期等待不是问题时才使用这种方法.

If you are sure that prompt will appear definitely and your scenario is ok to wait forever then using timeout=None in the expect will wait indefinitely for the prompt to appear. But yes waiting indefinitely has its own negative consequences. I would suggest to use this approach only if 100% sure that prompt will appear and waiting indefinitely is not a problem.

这篇关于pexpect 超时,来自孩子的大块数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆