简体   繁体   English

如何确定是否可以使用os.read(fd,[buffer [)进行读取而不会挂起?

[英]How to determine if an fd can be read with os.read(fd,[buffer[) without hanging?

Before doing: os.read(fd,1024) I would like to check that there will be output rather than it hanging until output it received. 在做之前: os.read(fd,1024)我想检查是否会有输出,而不是挂起直到收到输出为止。 Since fd is an int object, I can't do: 由于fd是一个int对象,所以我不能这样做:

os.fstat(f.fileno()).st_size

If I could get the size, I could check it is not 0. 如果我能得到大小,我可以检查一下它不为0。

Sorry if this is really simple, I am new to python. 抱歉,如果这真的很简单,我是python的新手。

Use select.select . 使用select.select (In windows, you can only it with socket): (在Windows中,只能将其与套接字一起使用):

import select

...

r, _, _ = select.select([fd], [], [], 0)
if r:
    data = os.read(fd, 1024)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM