Python os.set_blocking()
Python中的os.set_blocking()方法用于设置指定文件描述符的阻塞模式。此方法修改os.O_NONBLOCK标志。它为非阻塞模式设置了os.O_NONBLOCK标志,并为阻塞模式清除了os.O_NONBLOCK标志。
处于阻塞模式的文件描述符意味着像读、写或连接这样的I/O系统调用可以被系统阻塞。
例如:如果我们在stdin上调用read system call,那么我们的程序将被阻塞(内核将把进程放入睡眠状态),直到要读的数据在stdin上实际上是可用的。
注意:os.set_blocking()方法只在Unix平台上可用。
语法:os.set_blocking(fd, block)
参数:
fd:要设置其阻塞模式的文件描述符。
blocking:布尔值。如果文件描述符被设置为阻塞模式,则为True;如果文件描述符被设置为非阻塞模式,则为false。
返回类型:此方法不返回任何值。
示例1
使用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
版权声明:本页面内容旨在传播知识,为用户自行发布,若有侵权等问题请及时与本网联系,我们将第一时间处理。E-mail:284563525@qq.com