小能豆

如何设置 raw_input 的时间限制

javascript

在 python 中,有没有一种方法可以在等待用户输入时计算时间,以便在 30 秒后raw_input()自动跳过该功能?


阅读 43

收藏
2024-07-20

共1个答案

小能豆

signal在 Python 中,您可以使用模块处理输入函数超时来实现用户输入超时。此方法适用于基于 Unix 的系统,但可能不直接适用于 Windows。以下是如何实现此方法的示例:

  1. 使用signal模块(基于Unix的系统):
import signal

class TimeoutException(Exception):
    pass

def timeout_handler(signum, frame):
    raise TimeoutException

# Set the signal handler for alarm
signal.signal(signal.SIGALRM, timeout_handler)

def input_with_timeout(prompt, timeout=30):
    signal.alarm(timeout)
    try:
        return input(prompt)
    except TimeoutException:
        print("\nInput timed out!")
        return None


finally:
        signal.alarm(0)  # Disable the alarm

# Prompt user for user-defined information with timeout
try:
    Name = input_with_timeout('What is your Name? ', timeout=30)
    if Name is not None:
        Desc = input_with_timeout('Describe yourself: ', timeout=30)
        print(f'Name: {Name}')
        print(f'Description: {Desc}')
    else:
        print("User did not input their name.")
except Exception as e:
    print(f"An error occurred: {e}")

此代码设置了一个 30 秒的计时器,供用户输入

  1. 使用threadingsys.stdin(跨平台):

另一种方法是

import threading
import time

im
import sys

class InputThread(threading.Thread):
    def __init__(self, prompt):


super().__init__()
        self.prompt = prompt
        self.
        self.prompt = prompt
        self.inp

        self.prompt = prompt


        self.prompt


input = None



def run(self):
        self.
        self.


input = input(self.prompt)

def input_with_timeout(prompt, timeout=30):
    input_thread = InputThread(prompt)
    input_thread.start()
    input_thread.join(timeout)

    input_thread = InputThread(prompt)
    input_thread.start()
    input_thread.join(timeout)


    input_thread = InputThread(prompt)
    input_thread.start()
    input_thread.join(timeou

    input_thread = InputThread(prompt)
    input_thread.start()
    input_thread.join(

    input_thread = InputThread(prompt)
    input_thread.start()
    input_threa

    input_thread = InputThread(prompt)
    input_thread.start()
    inpu

    input_thread = InputThread(prompt)
    input_thread.start()


    input_thread = InputThread(prompt)
    input_thread.st

    input_thread = InputThread(prompt)
    input_th

    input_thread = InputThread(prompt)
    i

    input_thread = InputThread(prompt

    input_thread = InputThread

    input_thread = 

    input_


if input_thread.is_alive():

        prin


print("\nInput timed out!")


return None

    retu


return input_thread.input

# Prompt user for user-defined information with timeout

tr
try:
    Name = input_with_timeout(
    Name = input_with_timeo

    Name = input_with

    Name = inpu

    Name 


'What is your Name? ', timeout=30)


if Name is not None:
        Desc = input_with_timeout(
        Desc = input_with_timeout(

        Desc = input_with_tim

        Desc = input_wit

        Desc = inpu

        Desc =

        D


'Describe yourself: ', timeout=30)

        pri




print(f'Name: {Name}')


print(f'Description: {Desc}')

    els


else:

        pri


print("User did not input their name.")

excep

e
except Exception as e:

    p


print(f"An error occurred: {e}")

在此代码中,InputThread班级input_with_timeout它是None

这种方法确保了不同操作系统之间的兼容性

2024-07-20