本文整理汇总了Python中os.pipe2方法的典型用法代码示例。如果您正苦于以下问题:Python os.pipe2方法的具体用法?Python os.pipe2怎么用?Python os.pipe2使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.pipe2方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _pipe2_by_pipe
# 需要导入模块: import os [as 别名]
# 或者: from os import pipe2 [as 别名]
def _pipe2_by_pipe(flags):
"""A ``pipe2`` implementation using :func:`os.pipe`.
``flags`` is an integer providing the flags to ``pipe2``.
.. warning::
This implementation is not atomic!
Return a pair of file descriptors ``(r, w)``.
"""
fds = os.pipe()
if flags & os.O_NONBLOCK != 0:
for fd in fds:
set_fd_status_flag(fd, os.O_NONBLOCK)
if flags & O_CLOEXEC != 0:
for fd in fds:
set_fd_flag(fd, O_CLOEXEC)
return fds
示例2: test_pipe2
# 需要导入模块: import os [as 别名]
# 或者: from os import pipe2 [as 别名]
def test_pipe2(self):
self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
self.assertRaises(TypeError, os.pipe2, 0, 0)
# try calling with flags = 0, like os.pipe()
r, w = os.pipe2(0)
os.close(r)
os.close(w)
# test flags
r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
self.addCleanup(os.close, r)
self.addCleanup(os.close, w)
self.assertFalse(os.get_inheritable(r))
self.assertFalse(os.get_inheritable(w))
self.assertFalse(os.get_blocking(r))
self.assertFalse(os.get_blocking(w))
# try reading from an empty pipe: this should fail, not block
self.assertRaises(OSError, os.read, r, 1)
# try a write big enough to fill-up the pipe: this should either
# fail or perform a partial write, not block
try:
os.write(w, b'x' * support.PIPE_MAX_SIZE)
except OSError:
pass
示例3: test_pipe2
# 需要导入模块: import os [as 别名]
# 或者: from os import pipe2 [as 别名]
def test_pipe2(self):
self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
self.assertRaises(TypeError, os.pipe2, 0, 0)
# try calling with flags = 0, like os.pipe()
r, w = os.pipe2(0)
os.close(r)
os.close(w)
# test flags
r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
self.addCleanup(os.close, r)
self.addCleanup(os.close, w)
self.assertFalse(os.get_inheritable(r))
self.assertFalse(os.get_inheritable(w))
self.assertTrue(fcntl.fcntl(r, fcntl.F_GETFL) & os.O_NONBLOCK)
self.assertTrue(fcntl.fcntl(w, fcntl.F_GETFL) & os.O_NONBLOCK)
# try reading from an empty pipe: this should fail, not block
self.assertRaises(OSError, os.read, r, 1)
# try a write big enough to fill-up the pipe: this should either
# fail or perform a partial write, not block
try:
os.write(w, b'x' * support.PIPE_MAX_SIZE)
except OSError:
pass
示例4: _pipe2_ctypes
# 需要导入模块: import os [as 别名]
# 或者: from os import pipe2 [as 别名]
def _pipe2_ctypes(libc, flags):
"""A ``pipe2`` implementation using ``pipe2`` from ctypes.
``libc`` is a :class:`ctypes.CDLL` object for libc. ``flags`` is an
integer providing the flags to ``pipe2``.
Return a pair of file descriptors ``(r, w)``.
"""
fds = fd_pair()
libc.pipe2(fds, flags)
return fds[0], fds[1]
示例5: _get_pipe2_implementation
# 需要导入模块: import os [as 别名]
# 或者: from os import pipe2 [as 别名]
def _get_pipe2_implementation():
"""Find the appropriate implementation for ``pipe2``.
Return a function implementing ``pipe2``."""
if hasattr(os, 'pipe2'):
return os.pipe2 # pylint: disable=no-member
else:
try:
libc = load_ctypes_library("libc", SIGNATURES, ERROR_CHECKERS)
return (partial(_pipe2_ctypes, libc)
if hasattr(libc, 'pipe2') else
_pipe2_by_pipe)
except ImportError:
return _pipe2_by_pipe
示例6: test_pipe2_c_limits
# 需要导入模块: import os [as 别名]
# 或者: from os import pipe2 [as 别名]
def test_pipe2_c_limits(self):
# Issue 15989
import _testcapi
self.assertRaises(OverflowError, os.pipe2, _testcapi.INT_MAX + 1)
self.assertRaises(OverflowError, os.pipe2, _testcapi.UINT_MAX + 1)
示例7: _read_pages2
# 需要导入模块: import os [as 别名]
# 或者: from os import pipe2 [as 别名]
def _read_pages2(book, background=False):
'''Index `book`.
If `background` is True, yield during long operations.
If `book` is already loaded, does nothing.
Upon return `book` will be in state DONE or FAILED.'''
if book.filename == manual_filename:
return book
if book.load_state == book_file.LoadState.DONE:
return book
try:
(r, w) = os.pipe2(os.O_CLOEXEC)
indexer.trigger_load(book.filename, w)
buf = None
if background:
os.set_blocking(r, False)
while not buf:
try:
buf = os.read(r, 10)
except BlockingIOError:
await asyncio.sleep(0)
else:
os.set_blocking(r, True)
buf = os.read(r, 10)
if buf != b'ok':
raise BookFileError(
'loading failed: {}'.format(book.filename))
log.info('loading complete for {}'.format(book.filename))
bookmarks = book.bookmarks
if indexer.get_page_count(book.filename) > 1:
# add an end-of-book bookmark
bookmarks += (indexer.get_page_count(book.filename) - 1,)
return book._replace(load_state=book_file.LoadState.DONE,
bookmarks=bookmarks,
num_pages=indexer.get_page_count(book.filename),
indexed=True)
except Exception:
log.warning('book loading failed for {}'.format(book.filename))
return book._replace(load_state=book_file.LoadState.FAILED)
示例8: pipe2
# 需要导入模块: import os [as 别名]
# 或者: from os import pipe2 [as 别名]
def pipe2():
try:
read_fd, write_fd = os.pipe2(os.O_NONBLOCK | os.O_CLOEXEC)
except AttributeError:
import fcntl
read_fd, write_fd = os.pipe()
for fd in (read_fd, write_fd):
flag = fcntl.fcntl(fd, fcntl.F_GETFD)
fcntl.fcntl(fd, fcntl.F_SETFD, flag | fcntl.FD_CLOEXEC)
flag = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, flag | os.O_NONBLOCK)
return read_fd, write_fd
示例9: pipe_no_wait
# 需要导入模块: import os [as 别名]
# 或者: from os import pipe2 [as 别名]
def pipe_no_wait():
""" Generate a non-block pipe used to fetch the STDERR of ffmpeg.
"""
if platform == "linux" or platform == "linux2" or platform == "darwin" or platform.startswith("openbsd"):
import fcntl
import os
pipe_rd = 0
pipe_wd = 0
if hasattr(os, "pipe2"):
pipe_rd, pipe_wd = os.pipe2(os.O_NONBLOCK)
else:
pipe_rd, pipe_wd = os.pipe()
try:
fl = fcntl.fcntl(pipe_rd, fcntl.F_GETFL)
fcntl.fcntl(pipe_rd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
except:
print(sys.exc_info()[1])
return None, None
return pipe_rd, pipe_wd
elif platform == "win32":
# https://stackoverflow.com/questions/34504970/non-blocking-read-on-os-pipe-on-windows
import msvcrt
import os
from ctypes import windll, byref, wintypes, WinError, POINTER
from ctypes.wintypes import HANDLE, DWORD, BOOL
pipe_rd, pipe_wd = os.pipe()
LPDWORD = POINTER(DWORD)
PIPE_NOWAIT = wintypes.DWORD(0x00000001)
ERROR_NO_DATA = 232
SetNamedPipeHandleState = windll.kernel32.SetNamedPipeHandleState
SetNamedPipeHandleState.argtypes = [HANDLE, LPDWORD, LPDWORD, LPDWORD]
SetNamedPipeHandleState.restype = BOOL
h = msvcrt.get_osfhandle(pipe_rd)
res = windll.kernel32.SetNamedPipeHandleState(h, byref(PIPE_NOWAIT), None, None)
if res == 0:
print(WinError())
return None, None
return pipe_rd, pipe_wd