【问题标题】:Python sys.stdin.read(max) blocks until max is read (if max>=0), blocks until EOF else, but select indicates there is data to be readPython sys.stdin.read(max) 阻塞直到 max 被读取(如果 max>=0),阻塞直到 EOF 否则,但 select 表示有数据要读取
【发布时间】:2013-09-21 20:34:08
【问题描述】:

我的问题是:

select 表示有要读取的数据,我想读取那里的任何内容,我不想等待max 的数量出现。如果max max>0 读取块直到可以读取max 字节。

我不想要这个,我想阅读任何数量,选择将它放在“准备阅读”列表中。 read(1) 是不切实际的,因为这将涉及到大量的读取调用。但它不能阻塞。

有没有办法在 select 返回时找出缓冲区中存在的数量(如果它返回表明可以读取某些内容,而不是超时)并读取该数量?有没有办法像使用套接字一样使用max?它在哪里读取尽可能多的内容,然后返回?

解决方案可能是将文件置于非阻塞模式以进行读取?我不确定,我没想到会出现这种“直到 EOF”的行为。

我会继续阅读和尝试,但我只花了 30 分钟左右并没有接近,这就是我吸引你的原因。

注意

有很多问题询问如何让 recv 等待大量输入,并在达到最大值之前阻止事情,我不是在寻找这个。我的问题是它阻塞的。

附录

setblocking(False) 不起作用,我现在正在阅读如何使其在读取期间不阻塞。文档给了我希望:

stdin.read Found at: sys
read([size]) -> read at most size bytes, returned as a string.

If the size argument is negative or omitted, read until EOF is reached.
Notice that when in non-blocking mode, less data than what was 
 requested
may be returned, even if no size parameter was given.

附录二

看来read(0)实际上读到了0,那没什么,这导致死循环,有意思!

很抱歉,我似乎并没有真正尝试 0,我从 4096 开始(认为它会读取那里的任何内容....)尝试不带参数,但不带 0。

我想知道 read(0) 有什么用?

附录三

我现在遇到了 select 问题(我尝试将 read(1) 作为解决方案) 下面是实际代码:

def getInput(self):
    log.log(log.INFO,"GetInput","Select")
    readsReady = select.select((sys.stdin,),(),(),1)[0]
    if len(readsReady) == 0:
        #timed out
        log.log(log.INFO,"GetInput","Select timed out")
        if not self.toClose:
            self.handler.post("GetInput")
        else:
            threads.getCurrentThread().removeAllHandlers()
    else:
        #OPTIMISED FOR READING 1
        #log.log(log.INFO,"GetInput","Reading")
        data = sys.stdin.read(1)
        log.log(log.INFO,"GetInput","Read: "+data)

        if data == "\n":
            self.onInputHandler.post("OnInput",self.buffer)
            self.buffer=""
        else:
            self.buffer+=data
        self.handler.post("GetInput")

以下输出中与此无关的内容很少,它们是“Hello world!”几乎立即通过,“测试!”大约5秒。 “hello”是我输入的内容,输入,“k”是我稍后输入的内容,输入后我按回车一次。

输出:

0.0147    Verbose        1   SocketReader                        Created reader
0.0156    Verbose        2   SocketWriter                        Created writer
0.0260    Information    0   SocketReadWriter                    Created and ready for: ('localhost', 8294)
0.0268    Information    3   GetInput                            Select
Hello World!
1.0281    Information    3   GetInput                            Select timed out
1.0584    Information    3   GetInput                            Select
2.0593    Information    3   GetInput                            Select timed out
2.0896    Information    3   GetInput                            Select
3.0900    Information    3   GetInput                            Select timed out
3.1203    Information    3   GetInput                            Select
4.1215    Information    3   GetInput                            Select timed out
4.1519    Information    3   GetInput                            Select
TEST!
5.1524    Information    3   GetInput                            Select timed out
5.1828    Information    3   GetInput                            Select
hello
6.1467    Information    3   GetInput                            Read: h
6.1770    Information    3   GetInput                            Select
7.1782    Information    3   GetInput                            Select timed out
7.2086    Information    3   GetInput                            Select
8.2098    Information    3   GetInput                            Select timed out
8.2401    Information    3   GetInput                            Select
9.2414    Information    3   GetInput                            Select timed out
9.2717    Information    3   GetInput                            Select
10.2723   Information    3   GetInput                            Select timed out
10.3026   Information    3   GetInput                            Select
k
10.7939   Information    3   GetInput                            Read: e
10.8243   Information    3   GetInput                            Select
10.8245   Information    3   GetInput                            Read: l
10.8547   Information    3   GetInput                            Select
10.8549   Information    3   GetInput                            Read: l
10.8851   Information    3   GetInput                            Select
10.8853   Information    3   GetInput                            Read: o
10.9155   Information    3   GetInput                            Select
10.9157   Information    3   GetInput                            Read: 

10.9459   Information    3   GetInput                            Select
10.9461   Information    3   GetInput                            Read: k
10.9763   Information    3   GetInput                            Select
You said: hello
11.9775   Information    3   GetInput                            Select timed out
12.0123   Information    3   GetInput                            Select
13.0133   Information    3   GetInput                            Select timed out
13.0437   Information    3   GetInput                            Select
^C13.3985   Verbose        2   Threads                             Thread: 2 has ended
14.0442   Information    3   GetInput                            Select timed out
14.0746   Information    3   GetInput                            Select
14.3622   Verbose        1   Threads                             Thread: 1 has ended
15.0758   Information    3   GetInput                            Select timed out
15.1363   Information    3   GetInput                            Select
16.1373   Information    3   GetInput                            Select timed out
16.1677   Verbose        3   Threads                             Thread: 3 has ended

这里更容易看到:http://pastebin.com/raw.php?i=H6UHHmy8

有什么奇怪的?

它读取 hello 的“h”,但在 k 发生之前不会读取“hello\n”,如果有意义的话,它总是在 1 个换行符之前 1 个字母。

多次调用 select 会导致问题吗? (在另一个线程中,socket reader 也使用 select)

日志格式为:

*程序启动后的时间

*日志级别

*线程 ID(在运行线程中唯一)

*日志标签

*记录消息

Handler 类的作用

允许线程安全地相互发布消息,处理程序检查队列(以及任何预定事件在特定时间发生,例如 TEST,它发生在不同的线程上不用担心),发布"GetInput" 安排另一个对 GetInput 的调用,将它放在队列的后面。 “OnInput”消息被传递给另一个线程的处理程序,我想处理输入。

我这样做是因为它提供了一种处理线程的好方法,并且意味着我有很好的可重用代码(如 SocketReadWriter),我希望这不会导致对我的线程模型的批评,但是它确实有效。问题在于我尝试获取用户输入。

当我执行 ctrl+c 时,您还可以看到事情关闭了,这就是 toClose 事情的作用。当 select 超时时,如果它应该关闭,它将关闭。线程在没有处理程序时结束(处理程序仅在线程要运行的函数返回后接管,此函数可能会创建一个具有成员处理程序的类,因此当构造函数返回时,函数返回,有一个处理程序保持类活着)

解决方法

def getInput(self):
    log.log(log.INFO,"GetInput","Select")
    if sys.stdin.closed:
        readsReady = []
    else:
        readsReady = select.select((sys.stdin,),(),(),1)[0]
    if len(readsReady) == 0:
        #timed out
        log.log(log.INFO,"GetInput","Select timed out")
        if not self.toClose:
            self.handler.post("GetInput")
        else:
            threads.getCurrentThread().removeAllHandlers()
    else:
        data = sys.stdin.readline()
        if len(data) == 0:
            log.log(log.WARN,"GetInput","No data was returned indicating the file was closed")
            self.handler.post("GetInput") #if this is a close event, the next
            #timeout will deal with it
            return
        if data[-1] == "\n":
            data = data[:-1]
        log.log(log.INFO,"GetInput","Read: "+data)
        self.onInputHandler.post("OnInput",data)
        #if data == "\n":
        #   self.onInputHandler.post("OnInput",self.buffer)
        #   self.buffer=""
        #else:
        #   self.buffer+=data
        self.handler.post("GetInput")

def onClose(self):
    #log.log(log.WARN,"Input: OnClose","Called")
    self.toClose = True
    sys.stdin.close()

【问题讨论】:

  • 从标准输入读取是很挑剔的,因为它是缓冲的,你可以试试tty.setraw(sys.stdin.fileno())弄乱你的终端)看看是否有帮助。请注意,字符将停止回显到标准输出。
  • @Veedrac 是的,顺便说一句,readline 几乎可以正常工作!我现在正在看docs.python.org/2/library/curses.html。令人讨厌的是同样的事情在 C++ 中也有效。此外,输入可能并不总是终端。这就是为什么我想阅读那里的内容。我有那个“\n”部分只是因为我遇到了这个问题,并开始“手动”(使用终端)。

标签: python file file-io


【解决方案1】:

os 模块中有os.read 函数允许对从文件描述符读取进行较低级别的控制。只要至少有一个字节可供读取,它就是非阻塞的。

os.read(fd, n)

从文件描述符 fd 中最多读取 n 个字节。返回包含读取的字节的字符串。如果 fd 引用的文件结尾 已到达,返回一个空字符串。

可用性:Unix、Windows。

注意:此函数用于低级 I/O,并且必须应用于 os.open()pipe() 返回的文件描述符。要读取由内置函数open()popen()fdopen()sys.stdin 返回的“文件对象”,请使用其read()readline() 方法。

【讨论】:

  • 如果这可行,非常感谢,我不知道 read 和 read 是不同的功能!误导性文件 IMO
  • 它有效地解决了我使用 stackoverflow.com/a/2409034/5623476 获得的阅读箭头键的问题(它们会生成多个字符)。
猜你喜欢
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-14
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多