简体   繁体   English

Python:Cygwin中从文件/流的非阻塞读取

[英]Python: Non-blocking read from file/stream in Cygwin

If I was on a Unix system, then I could do something like this in Python to open a file in non-blocking mode: 如果我使用的是Unix系统,则可以在Python中执行以下操作,以非阻塞模式打开文件:

fd = os.open(filepath, os.O_NONBLOCK)

However, in Windows, os.O_NONBLOCK does not exist, and one would get an os module error 'module' object has no attribute O_NONBLOCK , if we tried to use it. 但是,在Windows中, os.O_NONBLOCK不存在,并且如果我们尝试使用os.O_NONBLOCK ,则会出现os模块错误'module' object has no attribute O_NONBLOCK情况。 We would have to do something like this for non-blocking input: https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/terminal/win32_input.py#L99 对于非阻塞输入,我们将必须执行以下操作: https : //github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/terminal/win32_input.py#L99

How does non-blocking input work in Cygwin though? 非阻塞输入在Cygwin中如何工作? Does Cygwin emulate something similar to the O_NONBLOCK option? Cygwin是否模拟类似于O_NONBLOCK选项的内容?

O_NONBLOCK can be used in C programming. O_NONBLOCK可以在C编程中使用。 You can see the definitions in the header file /usr/include/sys/_default_fcntl.h 您可以在头文件/usr/include/sys/_default_fcntl.h中查看定义。

#define _FNONBLOCK  0x4000  /* non blocking I/O (POSIX style) */
#define O_NONBLOCK  _FNONBLOCK

You could try the magic number 0x4000 directly. 您可以直接尝试魔术数字0x4000。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM