Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么白鹭';s stdout没有通过管道?_Python_Shell_Pipe_Grep - Fatal编程技术网

Python 为什么白鹭';s stdout没有通过管道?

Python 为什么白鹭';s stdout没有通过管道?,python,shell,pipe,grep,Python,Shell,Pipe,Grep,关于白鹭和烟斗,我有个奇怪的问题 我尝试过滤一个流,其中包含一些以主题名称开头的行,例如 “勾号:这是一条勾号消息\n” 当我尝试使用egrep对其进行过滤时: ./stream|u生成器| egrep'TICK'|。/topic|处理器 主题处理器似乎从未收到任何消息 但是,当我使用以下python脚本时: ./stream|u生成器| python filter.py--主题勾选|。/topic|处理器 一切看起来都很好 我想白鹭也需要一个“冲洗”机制,对吗 这里有人能给我一个线索吗?万分感

关于白鹭和烟斗,我有个奇怪的问题

我尝试过滤一个流,其中包含一些以主题名称开头的行,例如 “勾号:这是一条勾号消息\n”

当我尝试使用egrep对其进行过滤时: ./stream|u生成器| egrep'TICK'|。/topic|处理器 主题处理器似乎从未收到任何消息

但是,当我使用以下python脚本时: ./stream|u生成器| python filter.py--主题勾选|。/topic|处理器 一切看起来都很好

我想白鹭也需要一个“冲洗”机制,对吗

这里有人能给我一个线索吗?万分感谢

import sys
from optparse import OptionParser

if __name__ == '__main__':

    parser = OptionParser()

    parser.add_option("-m", "--topics",
                  action="store", type="string", dest="topics")

    (opts, args) = parser.parse_args()

    topics = opts.topics.split(':')

    while True:
        s = sys.stdin.readline()
        for each in topics:
            if s[0:4] == each:
                sys.stdout.write(s)
                sys.stdout.flush()

您是否允许命令
/stream_generator | egrep'TICK'|/topic_processor
运行到完成?如果命令已完成而未产生输出,则问题不在于缓冲,因为在
/stream\u generator
终止后,
egrep
将刷新其任何缓冲区,然后终止

现在,
egrep
在不直接输出到终端(即,输出到管道或文件时)时确实会使用重缓冲,并且如果
egrep
的缓冲区中没有积累足够的数据来保证刷新,那么可能会有一段时间,
egrep
不会产生输出。在GNU
egrep
中,可以使用
--line buffered
选项更改此行为:

./stream_generator | egrep --line-buffered 'TICK' | ./topic_processor 

如果你只做
/stream\u generator | egrep'TICK'
,你会得到什么?您是否在控制台上获得预期的结果输出?