【发布时间】:2014-09-11 01:16:29
【问题描述】:
我正在尝试等到一些文本被写入 Python 中的实时日志文件。
fdpexect 似乎是正确的选择,但它并没有等待。一旦到达文件末尾,它就会终止。
我想知道 fdpexpect 是否不支持这个,我需要解决它吗?
我的代码基本上是这样的:
创建生成对象:
# we're not using pexpect.spawn because we want
# all the output to be written to the logfile in real time,
# which spawn doesn't seem to support.
p = subprocess.Popen(command,
shell=shell,
stdout=spawnedLog.getFileObj(),
stderr=subprocess.STDOUT)
# give fdspawn the same file object we gave Popen
return (p, pexpect.fdpexpect.fdspawn(spawnedLog.getFileObj()))
等待某事:
pexpectObj.expect('something')
这基本上会在 'something' 事件发生并出现 EOF 错误之前立即退出。
【问题讨论】:
-
好吧,documentation of fdpexpect 是这样说的:“这允许您将 Pexpect 与套接字和命名管道 (FIFO) 一起使用。” 我假设没有“文件” " 从该列表中表示它不受支持。
-
当然可以,但是前面的那一行说“但是它可以与您传递的任何文件描述符一起使用。”
-
是的,但我认为问题在于套接字和命名管道在连接关闭之前不会发送 EOF。普通文件将在您读取整个文件后立即发送 EOF - 没有任何机制可以使
pexpect不断检查文件是否有新内容写入。 -
好的。除了“产生一个'tail -f
'进程并期待它”之外,还有什么想法? -
你能不能用
tee写到stdout和日志文件,例如pexpect.spawn("command | tee log.log")?
标签: python python-2.7 pexpect