Python:在Cygwin中从文件/流读取非阻塞 [英] Python: Non-blocking read from file/stream in Cygwin

查看:584
本文介绍了Python:在Cygwin中从文件/流读取非阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Unix系统上,那么我可以在Python中使用这样的方式在非阻塞模式下打开一个文件:

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)

但是,在Windows中, os.O_NONBLOCK 不存在,并且会得到 os 模块错误'module'对象没有属性O_NONBLOCK ,如果我们试图使用它。对于非阻塞输入,我们必须这样做: https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/terminal/win32_input.py#L99

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

非阻塞输入如何在Cygwin工作? Cygwin是否模拟类似 O_NONBLOCK 选项?

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

推荐答案

O_NONBLOCK可用于C编程。您可以在头文件/usr/include/sys/_default_fcntl.h中查看定义

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

神奇数字0x4000直接。

You could try the magic number 0x4000 directly.

这篇关于Python:在Cygwin中从文件/流读取非阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆