Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input timer
#1
can someone help me with an input timer I have tried 100 different ways to make it work and im at a loss.
Reply
#2
What is an input timer? What do you want it to do, and what have you tried so far?
Reply
#3
ill post a link of something i had posted the problem is if the time runs out it does not reset the loop properly so i have tired other ways but they all have failed

https://python-forum.io/Thread-input-timer
Reply
#4
Need your code post, it is unclear what you are trying to do. For example, are you trying to wait for the user to make an entry, but do something if they dont within a specific time?
Reply
#5
@jefsummers the link to my code is right above your comment. the code is on a different post already made with more detail on what I'm trying to do I'm re asking it maybe i posted it at a bad time i got no replies on it.
Reply
#6
First, these don't seem to really be "wake words" if you have to process them. They're more like guard words as a check. But it looks to me like both the wake and the control are coming in the same input channel.

I can think of a couple of approaches.

One is write it multithreaded. Have one thread get the input (which can block), but then put the info into a queue for the other thread to read. The other thread can timeout if the queue doesn't give any information in the time.

A perhaps simpler way is to loop over input, but then just check a timer for when the input arrived. If it's been too long since the last input, only process it as a wake word. If the time has been short, process as control. The disadvantage is that you can't report that you're transitioning to "asleep", because you're blocking on the input.

import time

WAIT_TIME = 5  # max wait time in seconds
WAKE_WORDS = ["Hello", "Mornin", "Howdy", "Hi"]

last_input = 0
state = "asleep"
while True:
    text = input()
    input_time = time.time()

    if input_time - last_input > WAIT_TIME:
        state = "asleep"
    last_input = input_time

    if state == "asleep":
        if text in WAKE_WORDS:
            print("Hello, what can I do for you?")
            state = "awake"
        else:
            print("  debug: wake word not found while asleep. ")
    elif state == "awake":
        print(f"I am processing {text} now")
Output:
$ python3 timerloop.py not wake word debug: wake word not found while asleep. Hi Hello, what can I do for you? dothis I am processing dothis now dothis # typed after waiting 5 seconds debug: wake word not found while asleep.
Reply
#7
I think i get what you are saying about the multithreaded approach and I like it. im going to do some research on it. You have given me some stuff to thinking about and definitely a good starting point to expand on thank you.
Reply
#8
You might be trying to reinvent the wheel.

you can use the schedule module to set timed events, and it is just a matter of creating functions for it to run.
Reply
#9
(Nov-19-2020, 10:03 PM)Nickd12 Wrote: I think i get what you are saying about the multithreaded approach and I like it. im going to do some research on it. You have given me some stuff to thinking about and definitely a good starting point to expand on thank you.

This is an older page on it, but seems still relevant. As it explicitly uses a multithreaded approach, it might have some starting points. https://repolinux.wordpress.com/2012/10/...in-python/
Reply
#10
so ive been doing some research is the only way to do it with sys.stdin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  email timer/rss feed timer ndiniz 1 2,045 Feb-02-2021, 07:18 PM
Last Post: nilamo
  input timer Nickd12 0 1,629 Nov-18-2020, 12:31 AM
Last Post: Nickd12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020