Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thread not Working
#1
Thumbs Down 
I've created a CLI little adventure game written in Python. I've defined a function that returns False whether time.sleep() is done and then I created a thread that allows the user to input the answer while self.is_time_elapsed() is True. Here's the code:
    def if_time_elapsed(self,clock):
        time.sleep(clock)
        return False

    def main(self):
        while self.run:
            self.start_game()
            os.system('clear')
            print(Game.ascii_header)
            print(f'Welcome to this Adventure {self.usr_name}!') 
            time.sleep(2)
            print('<START STORY>\nA terrible Iceberg destroyed the cruise ship you had taken enjoying the holidays,\n you ended up in the water at the mercy of the waves and you have to save yourself. ')
            print('\n<You\'ve got 10 seconds to write out the word "CLING" to cling to a rock.>')
            t_timer = threading.Thread(target=self.if_time_elapsed,args=(10,))
            t_timer.start()
            while t_timer:
                usr_input = input('> ').lower().strip()
                if usr_input != 'cling':
                    print("SCRRRR! Wrong answer!")
The problem is that basically the program keeps asking the input without closing after timeout. I also tried with t_timer.run() insted of t_timer.start() but nothing.
Reply
#2
Your code is blocking waiting for the user to press the enter key, so the "while timer:" loop never loops. "while timer:" doesn't make much sense either because it will always evaluate to True. timer is a Thread object, not the status of the thread. Maybe timer.is_alive()?

But none of this matters much because of the way input() works. input() is going to wait for input. I don't know of a way to cancel input() other than killing the process it is running in. I've seen examples that use threads to read input in the background, but none of them really work. Sure they work for 1 input, but if you try to use the same idea a second time in the same process you have a problem.

I have not used it, but PyInputPlus appears to have an input where you can set a timeout.
Reply
#3
(Aug-01-2021, 04:22 PM)deanhystad Wrote: Your code is blocking waiting for the user to press the enter key, so the "while timer:" loop never loops. "while timer:" doesn't make much sense either because it will always evaluate to True. timer is a Thread object, not the status of the thread. Maybe timer.is_alive()?

But none of this matters much because of the way input() works. input() is going to wait for input. I don't know of a way to cancel input() other than killing the process it is running in. I've seen examples that use threads to read input in the background, but none of them really work. Sure they work for 1 input, but if you try to use the same idea a second time in the same process you have a problem.

I have not used it, but PyInputPlus appears to have an input where you can set a timeout.
I think I get what you said. So I can't do what I want to do with a thread object? Should I only use PyInputPlus or there's another way similiar to mine?
Reply
#4
I'm not ready to say can't, but I do not see a clean way to get around the problem input() blocking whatever process it runs in. The most obvious solution is to use a subprocess. You can terminate a subprocess and that would end the input() call. But if you can just use a module that does what you want, why not try that first?
michaelserra likes this post
Reply
#5
(Aug-03-2021, 02:52 PM)deanhystad Wrote: I'm not ready to say can't, but I do not see a clean way to get around the problem input() blocking whatever process it runs in. The most obvious solution is to use a subprocess. You can terminate a subprocess and that would end the input() call. But if you can just use a module that does what you want, why not try that first?
Thank you, I think I'll try with the module Shy
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,383 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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