Python| os.set_blocking() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.set_blocking()
方法用于设置指定文件描述符的阻塞模式。此方法修改os.O_NONBLOCK标志。它为非阻塞模式设置os.O_NONBLOCK标志并为阻塞模式清除os.O_NONBLOCK标志。
处于阻塞模式的文件描述符意味着 I/O 系统调用(如 read、write 或 connect)可以被系统阻塞。
例如:如果我们在stdin上调用 read 系统调用,那么我们的程序将被阻塞(内核会将进程置于睡眠状态),直到要读取的数据在stdin上实际可用。
注意: os.set_blocking()
方法仅在 Unix 平台上可用。
Syntax: os.set_blocking(fd, blocking)
Parameter:
fd: A file descriptor whose blocking mode is to be set.
blocking: A Boolean value. True if the file descriptor is to be put into blocking mode and false if the file descriptor is to be put into non-blocking mode.
Return Type: This method does not return any value.
os.set_blocking()
方法设置文件描述符的阻塞模式。 # Python program to explain os.set_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 current blocking mode
# of the file descriptor
# using os.get_blocking() method
print("Blocking Mode:", os.get_blocking(fd))
# Change the blocking mode
blocking = False
os.set_blocking(fd, blocking)
print("Blocking mode changed")
# Get the blocking mode
# of the file descriptor
# using os.get_blocking() method
print("Blocking Mode:", os.get_blocking(fd))
# close the file descriptor
os.close(fd)
# A False value for blocking
# mode denotes that the file
# descriptor has been put into
# Non-Blocking mode while True
# denotes that file descriptor
# is in blocking mode.
Blocking Mode: True
Blocking mode changed
Blocking Mode: False
参考: https://docs。 Python.org/3/library/os.html#os.set_blocking