Cancel during Python input

Asked 2 years ago, Updated 2 years ago, 22 views

I want to terminate the function after a certain period of time while executing input within the Python function.


def example ():
    input()#If you do not enter anything for n seconds, I want to stop the input and end the example function.

python

2022-09-22 15:58

1 Answers

18.01.29 13:37 Correction:

You read the question completely wrong.

I think you can solve this problem through multi-thread.

Threads are responsible for executing one code. I think we can make one thread that measures time and one thread that executes input() and implement it so that the input() thread ends when the thread that measures time ends after a specific time. Instead, this will end the function after a certain amount of time, rather than after a certain amount of time, when the input is completed. There's a slight difference from the direction you want.

 

Reference: Learn by example Python programming - thread

StackOverflow Similar Questions

 

Answers before modification:

I looked it up on Google and found answers to similar questions.

The respondent wrote the following code by calling up the start time, receiving the input, and comparing it with the current time.

(Source: Raspberry Pi Forum lenik Reply )


2022-09-22 15:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.