Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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:清除stdin缓冲区_Python_File Io_Buffer_Stdin - Fatal编程技术网

Python:清除stdin缓冲区

Python:清除stdin缓冲区,python,file-io,buffer,stdin,Python,File Io,Buffer,Stdin,这可能是个问题,但我找不到办法! 我需要清除python中的stdin缓冲区 假设我正在运行以下bash脚本: i=0 for (( ; ; )) do echo "$i" ((i++)) done 像这样从命令行运行:./loop.sh | python myProg.py 在myProg.py中,我希望有如下内容: count = 100 f = fileinput.input() while True: sleep(2) # clear stdin som

这可能是个问题,但我找不到办法! 我需要清除python中的stdin缓冲区

假设我正在运行以下bash脚本:

i=0
for (( ; ; ))
do
    echo "$i"
    ((i++))
done
像这样从命令行运行:./loop.sh | python myProg.py

在myProg.py中,我希望有如下内容:

count = 100
f = fileinput.input()
while True:
    sleep(2)
    # clear stdin somehow ...
    # and read the most recent 100 lines
    i = 0
    while i < count:
        myBuffer[i] = f.readline()
        if len(myBuffer[i]) > 0:
            i += 1
    print myBuffer
count=100
f=fileinput.input()
尽管如此:
睡眠(2)
#以某种方式清除stdin。。。
#阅读最近的100行
i=0
当我数的时候:
myBuffer[i]=f.readline()
如果len(myBuffer[i])>0:
i+=1
打印我的缓冲区
我不认为我能在它之前读到所有的行,因为它以很高的速率把它们吐出来,如果睡眠(只是为了测试atm机)只有几分钟,那看起来很愚蠢。。。有没有办法在python中设置stdin缓冲区大小?还是干脆截断/清除它?顺便说一句,我使用的是Python2,所以没有bufsize参数

我看了这里

有什么办法吗?但我也会尝试解除缓冲:

更新:
unbuffer或stdbuf没有运气…

为了防止其他人有这个问题,我已经进行了广泛的搜索,并得出结论,由于内核实现等原因,这是不可能的

我写了下面的工作来做我想做的。我创建了两个文件:textFilter.py和getLatest.py。基本上运行./loopPrinting.sh | python textFilter.py并获取最新的100行。我有理由相信这是原子的和可靠的(尽管如果不是,请告诉我!!!)。它创建了16个文件并改变了计数(我认为类似于苹果直播)

textFilter.py

import sys
import os
import time

def mainLoop(size):
    myBuffer = [0]*size
    count = 0

    while True:
        for i in range(size):
            myBuffer[i] = sys.stdin.readline()

        f = open('/home/development/textFilter/' + repr(count) + '.dat', 'w')
        f.write(''.join(myBuffer))
        f.close()

        f = open('/home/development/textFilter/count.dat~', 'w')
        f.write(repr(count))
        f.flush()
        os.fsync(f.fileno()) 
        f.close()
        time.sleep(0.01)
        os.rename('/home/development/textFilter/count.dat~','/home/development/textFilter/count.dat')

        count += 1
        count = count % 16

if __name__ == "__main__":
    try:
        mainLoop(int(sys.argv[1]))
    except Exception:
        mainLoop(100)
getLatest.py

import sys
import time

def getData():
    f = open('count.dat', 'r')
    count = f.read()
    f.close()

    count = int(count)
    oldCount = count

    while oldCount == count:
    time.sleep(0.1)
    f = open('count.dat', 'r')
    count = f.read()
    f.close()
    count = int(count)

    f = open(repr(count) + '.dat', 'r')
    data = f.readlines()
    f.close()

    for row in data:
    print row,

    return 0

if __name__ == "__main__":
    try:
    getData()
    except Exception as inst:
    sys.stderr.write('ERROR')
    sys.stderr.write(inst)