Wait for Input for a Certain Time

How to wait for an input for a set amount of time in python

From my experience setting a time limit on a function is surprisingly difficult.
It might seem as an over kill but the way I would do it is like this:
Say the function you want to limit is called NFCReader then

from multiprocessing import Process

# Create a process of the function
func_proc = Process(target=NFCReader)

# Start the process
func_proc.start()

# Wait until child process terminate or T seconds pass
func_proc.join(timeout=T)

# Check if function has finished and if not kill it
if func_proc.is_alive():
func_proc.terminate()

You can read more about python Processes here

Additionally Since you want to receive data from your Process you need to somehow be able to read a variable in one process and have it available in another, for that you can use the Manager object.

In your case you could to the following:

def NCFReader(info):
info['id'], info['text'] = reader.read()

def main():
... SOME LINES OF CODE ...
# Motion is detected, now we want to time the function NCFReader
info = Manager.dict()
info['id'] = None
info['text'] = None
nfc_proc = Process(target=NCFReader, args=(info,))
nfc_proc.start()
nfc_proc.join(timeout=T)
if nfc_proc.is_alive():
nfc_proc.terminate()
# PROCESS DID NOT FINISH, DO SOMETHING
else:
# PROCESS DID FINISH, DO SOMETHING ELSE

notice that the dictionary info is a dictionary that is shared among all Process, so if you would want to use it again make sure you reset it's values.

Hope this helps

Expect Script to wait for specific input for a certain amount of time

Looks like you want something like this: this is the most flexible way to expect something to happen multiple times, and we don't have to care about exactly how many times it happens.

expect -c '
set timeout 1200 ;# 20 minutes
spawn sh ./setupapp.sh
expect {
"PRESS <ENTER> TO CONTINUE:" {
send "\r"
exp_continue
}
"Waiting here:" {
send "\r"
exp_continue
}
timeout {
error "nothing happened after $timeout seconds"
}
"PRESS <ENTER> TO Exit Installation:" {
send "\r"
}
}
expect eof
'

That expect command waits for one of four things to happen. For the first 2, hit enter then keep waiting for another event. I added the "timeout" event in case you want to do something special there.

The "press enter to exit" block does not call "exp_continue". After it sends the carriage return, the enclosing expect command ends and we then wait for eof.

How to wait 2 seconds for input in Javascript

use "setTimeout".

var Timer;

function chChannel(pressed) {
clearTimeout(Timer);
Timer = setTimeout(function () {
if (lengthCS < 3) {
inputChannel += pressed;
lengthCS = inputChannel.length;
}
}, 2000);

// call function to change channel using variable value inputChannel for ex. 011
};

Wait set time for user input C# console app

Set a 10 second timer off.

Fire an event when the timer expires.

In the event handler proceed with the "auto run" section.

If the user hits a key before the timer expires, kill the thread.

The Timer class page on MSDN has an example of a timer waiting for a set period.

Keyboard input with timeout?

The example you have linked to is wrong and the exception is actually occuring when calling alarm handler instead of when read blocks. Better try this:

import signal
TIMEOUT = 5 # number of seconds your want for timeout

def interrupted(signum, frame):
"called when read times out"
print 'interrupted!'
signal.signal(signal.SIGALRM, interrupted)

def input():
try:
print 'You have 5 seconds to type in your stuff...'
foo = raw_input()
return foo
except:
# timeout
return

# set alarm
signal.alarm(TIMEOUT)
s = input()
# disable the alarm after success
signal.alarm(0)
print 'You typed', s

Time limit for an input

import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
public class test
{
private String str = "";

TimerTask task = new TimerTask()
{
public void run()
{
if( str.equals("") )
{
System.out.println( "you input nothing. exit..." );
System.exit( 0 );
}
}
};

public void getInput() throws Exception
{
Timer timer = new Timer();
timer.schedule( task, 10*1000 );

System.out.println( "Input a string within 10 seconds: " );
BufferedReader in = new BufferedReader(
new InputStreamReader( System.in ) );
str = in.readLine();

timer.cancel();
System.out.println( "you have entered: "+ str );
}

public static void main( String[] args )
{
try
{
(new test()).getInput();
}
catch( Exception e )
{
System.out.println( e );
}
System.out.println( "main exit..." );
}
}

How to have python waiting for input until input unchanged for a certain time

Do multi-threading. Threads allow you do some tasks simultaneously. I suggest you do the input-fetching operation as a thread and a timer operation on another thread. So now, you can check the timer while fetching the input and pause/resume the fetch when the timer reaches a particular limit.
In case you need references on multi-threading: Multi-Threading tutorial in tutorialspoint



Related Topics



Leave a reply



Submit