Python sys.stdin.read(max) 阻塞直到读到max(如果max>=0),阻塞直到EOF else,但是select表示有数据要读取

标签 python file file-io

我的问题是:
select表示有数据要读,有什么就读什么,不想等max存在的金额。如果 max <= 0 然后读取等待直到遇到 EOF,如果 max >0 读取块直到 max字节可以读取。

我不想要这个,我想阅读任何数量的 select 把它放在“准备阅读”列表中。 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 秒。
“你好”是我输入的东西,输入,“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 会导致问题吗? (在另一个线程中,套接字读取器也使用选择)

日志的格式为:

*自程序开始以来的时间

*日志级别

*线程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()

最佳答案

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

os.read(fd, n)

Read at most n bytes from file descriptor fd. Return a string containing the bytes read. If the end of the file referred to by fd has been reached, an empty string is returned.

Availability: Unix, Windows.

Note: This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open() or pipe(). To read a “file object” returned by the built-in function open() or by popen() or fdopen(), or sys.stdin, use its read() or readline() methods.

关于Python sys.stdin.read(max) 阻塞直到读到max(如果max>=0),阻塞直到EOF else,但是select表示有数据要读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18937338/

相关文章:

完成后自行删除的php脚本

php - file_put_contents - 无法打开流 : Permission denied

c - 执行结束时修改文件内容

python - 如何在Python中获取季度开始日期

python - 使用 Python 将输出总和四舍五入到最接近的偶数

python - 使用 pickle 编写列表列表

Java代码从文件夹中获取文件名列表

c - 在文件中查找单词?

python - 在 Django Rest 中捕获 URL 参数

python - 使用CoLab在Google云端硬盘上OpenCV读取和写入速度太慢