用python从xinput测试中读取stdout

用python从xinput测试中读取stdout,python,linux,input,pipe,xorg,Python,Linux,Input,Pipe,Xorg,我试图将xinput的输出流式传输到我的python程序中,但是我的程序只是等待并保持空白。我认为这可能与缓冲有关,但我不能说。运行xinputtest15可以让我移动鼠标,但这样做不会打印出来。顺便说一下,要找到你的鼠标ID,只需键入xinput,它就会列出你的设备 #!/usr/bin/env python import sys import subprocess # connect to mouse g = subprocess.Popen(["xinput", "test", str

我试图将xinput的输出流式传输到我的python程序中,但是我的程序只是等待并保持空白。我认为这可能与缓冲有关,但我不能说。运行xinputtest15可以让我移动鼠标,但这样做不会打印出来。顺便说一下,要找到你的鼠标ID,只需键入xinput,它就会列出你的设备

#!/usr/bin/env python
import sys
import subprocess


# connect to mouse
g = subprocess.Popen(["xinput", "test", str(mouse_id)], stdout=subprocess.PIPE)

for line in g.stdout:
    print(line)
    sys.stdout.flush()    

你的代码对我有用;但是,如果没有连接到tty,xinput cmd似乎会缓冲其输出。当运行代码时,继续移动鼠标,xinput最终会刷新stdout,您将看到您的行以块的形式显示。。。至少我在运行你的代码时做到了

我重新编写了您的代码以消除缓冲,但我无法让它不以块的形式出现,因此我认为xinput应该受到责备。当未连接到TTY时,它不会用每个新事件刷新标准输出缓冲区。这可以通过xinput测试15 | cat进行验证。移动鼠标将导致数据以缓冲块形式打印;就像你的代码一样

如果有帮助,我的测试代码如下

#!/usr/bin/python -u

# the -u flag makes python not buffer stdios


import os
from subprocess import Popen

_read, _write = os.pipe()

# I tried os.fork() to see if buffering was happening
# in subprocess, but it isn't

#if not os.fork():
#    os.close(_read)
#    os.close(1) # stdout
#    os.dup2(_write, 1)
#
#    os.execlp('xinput', 'xinput', 'test', '11')
#    os._exit(0) # Should never get eval'd

write_fd = os.fdopen(_write, 'w', 0)
proc = Popen(['xinput', 'test', '11'], stdout = write_fd)

os.close(_write)

# when using os.read() there is no readline method
# i made a generator
def read_line():
    line = []
    while True:
        c = os.read(_read, 1)
        if not c: raise StopIteration
        if c == '\n':
            yield "".join(line)
            line = []
            continue
        line += c



readline = read_line()

for each in readline:
    print each

你的代码对我有用;但是,如果没有连接到tty,xinput cmd似乎会缓冲其输出。当运行代码时,继续移动鼠标,xinput最终会刷新stdout,您将看到您的行以块的形式显示。。。至少我在运行你的代码时做到了

我重新编写了您的代码以消除缓冲,但我无法让它不以块的形式出现,因此我认为xinput应该受到责备。当未连接到TTY时,它不会用每个新事件刷新标准输出缓冲区。这可以通过xinput测试15 | cat进行验证。移动鼠标将导致数据以缓冲块形式打印;就像你的代码一样

如果有帮助,我的测试代码如下

#!/usr/bin/python -u

# the -u flag makes python not buffer stdios


import os
from subprocess import Popen

_read, _write = os.pipe()

# I tried os.fork() to see if buffering was happening
# in subprocess, but it isn't

#if not os.fork():
#    os.close(_read)
#    os.close(1) # stdout
#    os.dup2(_write, 1)
#
#    os.execlp('xinput', 'xinput', 'test', '11')
#    os._exit(0) # Should never get eval'd

write_fd = os.fdopen(_write, 'w', 0)
proc = Popen(['xinput', 'test', '11'], stdout = write_fd)

os.close(_write)

# when using os.read() there is no readline method
# i made a generator
def read_line():
    line = []
    while True:
        c = os.read(_read, 1)
        if not c: raise StopIteration
        if c == '\n':
            yield "".join(line)
            line = []
            continue
        line += c



readline = read_line()

for each in readline:
    print each
请看一看,特别是本教程

请看一看,特别是本教程


输出中是否有换行符?如果在一段时间后终止子流程,会发生什么情况?刷新g.stdout有效果吗?xinput测试为每个事件返回一个新行,对于terminate,它只给出键盘中断Aha,我认为您对g.stdout.flush的更改有效果。它现在输出,哇!现在唯一的问题是刷新需要几秒钟的时间,这意味着对于测试,我必须获取数百行(如果不是数千行的话),直到它们同时转储。是否有强制立即转储的方法,只要我能得到它,从控制台运行命令时没有延迟输出中是否有换行符?如果在一段时间后终止子流程,会发生什么情况?刷新g.stdout有效果吗?xinput测试为每个事件返回一个新行,对于terminate,它只给出键盘中断Aha,我认为您对g.stdout.flush的更改有效果。它现在输出,哇!现在唯一的问题是刷新需要几秒钟的时间,这意味着对于测试,我必须获取数百行(如果不是数千行的话),直到它们同时转储。是否有强制立即转储的方法,只要我能得到它,从控制台运行命令时就不会有延迟