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

标签 python

在执行之前:os.read(fd,1024) 我想检查是否会有输出,而不是挂起直到收到输出。由于 fd 是一个 int 对象,我不能这样做:

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

如果我可以获得大小,我可以检查它是否不是 0。

抱歉,如果这真的很简单,我是 python 新手。

最佳答案

使用select.select 。 (在Windows中,只能使用socket):

import select

...

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

关于python - 如何确定是否可以使用 os.read(fd,[buffer[) 读取 fd 而不会挂起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20455311/

相关文章:

struct.unpack 上的 Python MemoryError

python - pd.merge 时处理空值

python - 在 webhook 的头像上上传本地镜像时出现错误 '_io.BufferedReader' 对象没有属性 'startswith'

python - 根据时间戳间隔创建 csv 文件的数据框

python - 有没有办法在断点处获取 python 调试 session 的 'snapshot'?

python - 这个 while 循环是如何工作的?

python - 列表理解与 for 循环,在类定义中

Python字符串长度递归

python - 为什么代码检测到最外层)

python - 从 Python io.BufferedReader 类的实例读取/打开图像