Python os.get_blocking() - 用于获取指定文件描述符的阻塞模式信息

今夜星潮暗涌

Python os.get_blocking()

Python中的os.get_blocking()方法用于获取指定文件描述符的阻塞模式信息

如果os.O_NONBLOCK标志未设置,则此方法返回True,这意味着指定的文件描述符处于阻塞模式;如果设置了os.O_NONBLOCK标志,这意味着指定的文件描述符处于非阻塞模式,则返回False。

处于阻塞模式的文件描述符意味着像读、写或连接这样的I/O系统调用可以被系统阻塞。

例如:如果我们在stdin上调用read system call,那么我们的程序将被阻塞(内核将把进程放入睡眠状态),直到要读的数据在stdin上实际上是可用的。

注意:os.get_blocking()方法只在Unix平台上可用。

语法:os.get_blocking(fd)

参数:

fd:需要其阻塞模式信息的文件描述符。

返回类型:此方法返回类“bool”的布尔值。True表示文件描述符处于阻塞模式,False表示文件描述符处于非阻塞模式。

示例1

使用os.get_blocking()方法来获取文件描述符的阻塞模式

# Python program to explain os.get_blocking() method 
    
# importing os module 
import os
  
# File path 
path = "/home / ihritik / Documents / file.txt"
  
  
# Open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_RDWR)
  
  
# Get the blocking mode
# of the file descriptor
# using os.get_blocking() method
mode = os.get_blocking(fd)
  
if mode == True:
    print("File descriptor is in blocking mode")
else :
    print("File descriptor is in Non-blocking mode")
  
  
# Close the file descriptor
os.close(fd)

输出:

File descriptor is in blocking mode

版权声明:本页面内容旨在传播知识,为用户自行发布,若有侵权等问题请及时与本网联系,我们将第一时间处理。E-mail:284563525@qq.com

目录[+]

取消
微信二维码
微信二维码
支付宝二维码