Python | os.get_blocking 函數

判斷文件是否阻塞

最近更新時間 2020-12-10 11:01:10

os.get_blocking 函數獲取文件描述符的阻塞模式。如果文件未設置 os.O_NONBLOCK 標誌(表示指定的文件描述符處於阻塞模式),則此方法返回 True。如果設置了 os.O_NONBLOCK 標誌(表示指定的文件描述符處於非阻塞模式),則返回 False。

設置阻塞模式可參見 os.set_blocking(fd, blocking) 和 socket.setblocking(flag)。

在 Linux 系統中,處於阻止模式的文件描述符意味著系統可以阻止 I/O 系統調用(如讀取,寫入或連接)。

函數定義

os.get_blocking(fd)
# 函數定義

if sys.platform != 'win32':
    # Unix only
    ...
    def get_blocking(fd: int) -> bool: ...
    ...

兼容性:Unix 系統。

參數

  • checkfd - 文件描述符。

返回值

  • checkbool - 是否阻塞模式。

示例1: - 使用 os.get_blocking() 函數獲取文件阻塞模式狀態。

# coding=utf-8

# Python3 代碼
# 講解怎樣使用 os.get_blocking() 函數獲取文件阻塞模式

# 引入 os 庫
import os

# 文件路徑
path = "foo.txt"

# 使用 os.open 函數獲取文件描述符
fd = os.open(path, os.O_RDWR)

# 使用 os.get_blocking() 方法獲取文件阻塞模式
mode = os.get_blocking(fd)

print("File blocking mode::", mode)
File blocking mode:: True
rss_feed