Home computickets Keyboards with a timeout in Python. How to make that the program...

Keyboards with a timeout in Python. How to make that the program is not waiting forever?

Author

Date

Category

The program starts, and should default to wait 100 seconds for user response, but waiting forever until it is input. How to make that the program was only waiting for 100 seconds?

For example, in the program below, if the user entered 1 , the program is now performs the function, or the user does not enter anything, will wait 10 hours and then perform the function for the job. How to write a console program that works like this:

work ():
  print ( 'it's working right now ")
import time
print ( 'execute the program in 10 hours or 1 immediately - immediately * - 10 hours (do not enter) ")
s = input ( '')
if s == 1:
  work ()
ELSE:
  time.sleep ( '36000')
  work ()

Answer 1, Authority 100%

I created for this class wrapper by using threading :

from threading import Thread
class Inp:
  inp = None
  def __init __ (self):
    t = Thread (target = self.get_input)
    t.daemon = True
    t.start ()
    t.join (timeout = 100)
  def get_input (self):
    self.inp = input ()
  def get (self):
    return self.inp
inp = Inp (). get ()
print (f'Vy entered {inp} 'if inp else f'Vy not entered anything!')

However, this code is not perfect, the input turns “disposable.” We can continue the program, but if the user still did not entered, the console will still continue to wait for his input. In your case – this is not a problem, but the solution is non-universal

.


Answer 2, Authority 100%

https://stackoverflow.com/questions/15528939/python-3-timed -input / 15533404 # 15533404

from threading import Timer
def work ():
  print ( 'it's working right now ")
timeout = 36000
t = Timer (timeout, work) # Set up a countdown timer 10 hours at which time autu perform work function
t.start () # Start the timer
print ( 'executes the program in 10 hours or 1 immediately - immediately * - 10 hours (do not enter) ")
s = input ()
if s == "1": # If the user enters "1", then cancel the countdown timer and start the function at once
  t.cancel ()
  work ()

It should be borne in mind that if the 10:00 will take place, the function of work, and then the user enters “1”, the function will work again.


Answer 3, Authority 100%

In Python there is a library for this purpose timeout_decorator

With its use, your problem is solved so

import time
from timeout_decorator import timeout, TimeoutError
# Decorator for setting timeout on function execution
# Seconds - how musch seconds to wait
# Default - value that will be returned on timeout
def set_timeout (seconds, default = None):
  def _decorator (function):
    function = timeout (seconds) (function)
    def _wrapper (* args, ** kwargs):
      Try:
        return function (* args, ** kwargs)
      except TimeoutError:
        return default
    return _wrapper
  return _decorator
# Waits for function execution
# Function - function with timeout
# Seconds - how musch seconds to wait
# Default - value that will be returned on timeout
def wait_for (function, seconds, default = None):
  return set_timeout (seconds, default) (function)
def work ():
  print ( 'it's working right now ")
wait_input = wait_for (input, 100)
text = wait_input ()
if text == '1':
  work ()
elif text is None:
  print ( 'timeout')
  time.sleep (36000)
  work ()

As a result, the timeout can be set for any of the existing functions by using wait_for (a new feature, but with a timeout)

wait_input = wait_for (input, 100)

Or, for its function by a decorator set_timeout

@ set_timeout (100)
Def wait_input ():
  # Some More Code
  RETURN INPUT ()

And it is also possible to set the default returned value that returns if the user has not entered anything

wait_input = wait_for (input, 100, default = 'sleep')
Text = wait_input ()
If text == '1':
  Work ()
Elif Text == 'Sleep':
  Time.Sleep (36000)
  Work ()

Answer 4, Authority 100%

@ nomnoms12, I supplemented the code and this is what happened:

Import Time
From Threading Import Thread
Def Work ():
  Print ('works')
Class Inp:
  Inp = none
  Def __init __ (Self):
    T = Thread (target = self.get_input)
    T.Daemon = True.
    T.Start ()
    T.JOIN (Timeout = 5)
  DEF Get_input (Self):
    Self.Inp = Input ()
  DEF Get (Self):
    Return Self.Inp.
Print ('Enter anything and execute now, otherwise, after 1 hour ...')
INP = INP (). Get ()
IF Inp:
  Print (F'Vi entered {INP} ')
  Work ()
ELSE:
  Print (F'Vi did not enter anything! Follow 1 hour ')
  Time.Sleep (3600)
  Work ()

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions