ana*_*nik 9 python windows pipe
这个问题 - 如何从os.pipe()读取而不被阻止?- 显示如何检查os.pipe
Linux 是否有任何数据的解决方案,为此您需要将管道置于非阻塞模式:
import os, fcntl
fcntl.fcntl(thePipe, fcntl.F_SETFL, os.O_NONBLOCK)
Run Code Online (Sandbox Code Playgroud)
在Windows上我们有这个:
ImportError: No module named fcntl
Run Code Online (Sandbox Code Playgroud)
但是os.pipe
有:
>>> os.pipe()
(3, 4)
Run Code Online (Sandbox Code Playgroud)
那么,是否可以进行非阻塞读取或窥视os.pipe
Windows上的内容?
ana*_*nik 11
通过StackOverflow挖掘一段时间后回答我自己的问题.
更新:由于@HarryJohnston,事情发生了变化.
起初答案是否定的,不可能os.pipe
在Windows 上进行非阻塞读取.从这个回答我得到了:
Windows中非阻塞/异步I/O的术语是"重叠" - 这就是您应该关注的内容.
os.pipe
在Windows上通过CreatePipe
API 实现(参见此处 ......好吧,我os.pipe
在Python源代码中找不到代码).CreatePipe
使得匿名管道,匿名管道不支持异步I/O.
但是 @HarryJohnston评论说SetNamedPipeHandleState doc允许将匿名管道放到非阻塞模式.我写了测试但它失败了OSError: [Errno 22] Invalid argument
.错误消息似乎是错误的,所以我试图检查当数据不可用时非阻塞读取操作应该返回什么结果,并且在读取命名管道模式的MSDN注释后,我发现它应该ERROR_NO_DATA
具有int值232.添加ctypes.WinError()
对异常处理程序的调用揭示了预期[Error 232] The pipe is being closed.
所以,答案是肯定的,可以os.pipe
在Windows 上进行非阻塞读取,这是证明:
import msvcrt
import os
from ctypes import windll, byref, wintypes, GetLastError, WinError
from ctypes.wintypes import HANDLE, DWORD, POINTER, BOOL
LPDWORD = POINTER(DWORD)
PIPE_NOWAIT = wintypes.DWORD(0x00000001)
ERROR_NO_DATA = 232
def pipe_no_wait(pipefd):
""" pipefd is a integer as returned by os.pipe """
SetNamedPipeHandleState = windll.kernel32.SetNamedPipeHandleState
SetNamedPipeHandleState.argtypes = [HANDLE, LPDWORD, LPDWORD, LPDWORD]
SetNamedPipeHandleState.restype = BOOL
h = msvcrt.get_osfhandle(pipefd)
res = windll.kernel32.SetNamedPipeHandleState(h, byref(PIPE_NOWAIT), None, None)
if res == 0:
print(WinError())
return False
return True
if __name__ == '__main__':
# CreatePipe
r, w = os.pipe()
pipe_no_wait(r)
print os.write(w, 'xxx')
print os.read(r, 1024)
try:
print os.write(w, 'yyy')
print os.read(r, 1024)
print os.read(r, 1024)
except OSError as e:
print dir(e), e.errno, GetLastError()
print(WinError())
if GetLastError() != ERROR_NO_DATA:
raise
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10996 次 |
最近记录: |