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

陌路散爱 提交于 2019-12-10 12:06:47

问题


If I was on a Unix system, then I could do something like this in Python to open a file in non-blocking mode:

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. 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

How does non-blocking input work in Cygwin though? Does Cygwin emulate something similar to the O_NONBLOCK option?


回答1:


O_NONBLOCK can be used in C programming. You can see the definitions in the header file /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.



来源:https://stackoverflow.com/questions/34696267/python-non-blocking-read-from-file-stream-in-cygwin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!