You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下Python用select模块报错OSError: [WinError 10093]求助

Windows下Python限时输入代码报错原因及修复方案

问题根源

你这段代码在Windows上抛出OSError: [WinError 10093]的核心原因是:select.select()在Windows系统中仅支持监听套接字(socket)对象,无法对sys.stdin这类普通文件描述符进行操作。select模块是为Unix-like系统设计的,跨平台兼容性有限,Windows系统的实现做了严格限制。

修复方案

方案1:使用Windows专属msvcrt模块

利用Windows API提供的msvcrt模块实现按键检测和限时输入,这是Windows环境下最直接的解决方案:

import msvcrt
import time

print("Who is your best friend?")
print("You have ten seconds to answer!")

start_time = time.time()
input_content = ""
# 循环等待输入或超时
while time.time() - start_time < 10:
    if msvcrt.kbhit():  # 检测是否有按键按下
        input_content = msvcrt.readline().decode("utf-8").strip()
        break
else:
    print("Your time is over")
    exit()

print("You stated your best friend name as: ", input_content)

方案2:使用线程实现跨平台限时输入

如果需要代码同时兼容Windows和Unix系统,可以用多线程实现:主线程等待输入线程执行,超时则终止判定:

import sys
import threading

user_input = None

def read_input():
    global user_input
    user_input = sys.stdin.readline().strip()

print("Who is your best friend?")
print("You have ten seconds to answer!")

# 启动输入线程
input_thread = threading.Thread(target=read_input)
input_thread.daemon = True  # 设置守护线程,主线程结束时自动退出
input_thread.start()

# 主线程等待10秒
input_thread.join(timeout=10)

if user_input is not None:
    print("You stated your best friend name as: ", user_input)
else:
    print("Your time is over")

内容的提问来源于stack exchange,提问作者Swapravo Roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 21:33:12