please select a valid python_Python select()行为很奇怪

I'm having some trouble understanding the behavior of select.select. Please consider the following Python program:

def str_to_hex(s):

def dig(n):

if n > 9:

return chr(65-10+n)

else:

return chr(48+n)

r = ''

while len(s) > 0:

c = s[0]

s = s[1:]

a = ord(c) / 16

b = ord(c) % 16

r = r + dig(a) + dig(b)

return r

while True:

ans,_,_ = select.select([sys.stdin],[],[])

print ans

s = ans[0].read(1)

if len(s) == 0: break

print str_to_hex(s)

I have saved this to a file "test.py". If invoke it as follows:

echo 'hello' | ./test.py

then I get the expected behavior: select never blocks and all of the data is printed; the program then terminates.

But if I run the program interactively, I get a most undesirable behavior. Please consider the following console session:

$ ./test.py

hello

[', mode 'r' at 0xb742f020>]

68

The program then hangs there; select.select is now blocking again. It is not until I provide more input or close the input stream that the next character (and all of the rest of them) are printed, even though there are already characters waiting! Can anyone explain this behavior to me? I am seeing something similar in a stream tunneling program I have written and it's wrecking the entire affair.

Thanks for reading!

解决方案

The read method of sys.stdin works at a higher level of abstraction than select. When you do ans[0].read(1), python actually reads a larger number of bytes from the operating system and buffers them internally. select is not aware of this extra buffering; It only sees that everything has been read, and so will block until either an EOF or more input arrives. You can observe this behaviour by running something like strace -e read,select python yourprogram.py.

One solution is to replace ans[0].read(1) with os.read(ans[0].fileno(), 1). os.read is a lower level interface without any buffering between it and the operating system, so it's a better match for select.

Alternatively, running python with the -u commandline option also seems to disable the extra buffering.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当您在使用某些Python集成开发环境(IDE)或者命令行工具执行Python代码时,遇到 "Error: Please select a valid Python interpreter" 的错误,这通常意味着您没有正确地配置或选择IDE使用的Python解释器。可能的原因有: 1. **IDE设置问题**:在IDE(如PyCharm、VS Code、Atom等)中,可能没有设置默认的Python路径,或者指定的Python版本不在系统路径中。 2. **环境变量问题**:操作系统环境变量可能未正确配置,比如`PATH`变量中没有包含Python安装目录下的`Scripts`文件夹。 3. **多重Python安装**:如果您的系统上安装了多个Python版本,而IDE或工具没有明确选择一个,就会出现这个错误。 4. **Python解释器已损坏**:如果Python解释器文件本身存在损坏,可能也会导致无法识别。 为了解决这个问题,您可以尝试以下步骤: 1. **检查IDE设置**:确保IDE中的Python配置选项指向正确的Python解释器路径。 2. **更新环境变量**:在控制面板或终端中更新系统的`PATH`环境变量。 3. **选择正确的Python解释器**:在IDE中,手动选择合适的Python版本。 4. **清理或修复Python安装**:卸载并重新安装Python,或者使用`py -m pip install --upgrade --force-reinstall python`来修复可能的问题。 5. **检查路径是否存在**:确保Python可执行文件(如python.exe或python3.exe)位于指定路径下。 如果您能提供更具体的环境和操作步骤,我可以给出更准确的解决方案。相关问题: 1. 如何在PyCharm中设置Python路径? 2. 如何在Windows系统中查看和编辑环境变量? 3. 如果有两个Python版本,如何在VS Code中切换它们?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值