-1
# player is a class object (class Game)
def the_hunt(target_number, player):    
    timer_thread = Thread(target=timer, args=(player, ))
    timer_thread.start()
    while True:
        # when time is zero sec it will terminate the game
        if not timer_thread.is_alive():
            print("The Hunt is Over. You ran out of time!")
            player.game_status(False)
            return False
        
        # check for the win
        if player._score == player.win_points:
            timer_thread.cancel()
            player.game_status(True)
def timer(obj: Game):
    sleep_duration = 60
    while sleep_duration > 0:
        time.sleep(1)        
        obj.duration_reduce(1)
        sleep_duration -= 1

error I get AttributeError: 'Thread' object has no attribute 'cancel' when I used timer_thread = Thread(target=timer) instead of timer_thread = Thread(target=timer, args=(player, )) and def timer(): it worked but with the given code where I have used class object, it gives an error. I am not using threading in class just passing a value to the class

  • 1
    Threads doesn't have `cancel`... and you shouldn't kill it by yourself https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread – Guy Sep 18 '22 at 08:41
  • Your explanation of the problem (when it works and when not) is not the clearest. It would be useful that you provide the working code example and the one that does not work, to compare all easier. – elano7 Sep 18 '22 at 08:58
  • In your own words, why should the code `timer_thread.cancel()` mean anything? What do you expect to happen at that line of code, and **why**? Did you try to [**read the documentation**](https://docs.python.org/3/library/threading.html#thread-objects) for the `Thread` class, in order to understand what methods it has? Also: what exactly what unclear about the error message? For example, do you understand what `attribute` means? If not, [did you try](https://meta.stackoverflow.com/questions/261592) to [look it up](https://duckduckgo.com/?q=python+what+is+an+attribute)? – Karl Knechtel Sep 18 '22 at 09:24
  • "when I used... it worked" **No, it did not**. It may have *appeared to* work, because the code in the thread running `the_hunt` didn't get to that point yet. But the error message is telling you exactly what line of code is causing the problem and where it is, and that code is obviously nothing to do with the `timer` function. – Karl Knechtel Sep 18 '22 at 09:27
  • when in def the_hunt(): i use timer_thread = Thread(target=timer) and do the def timer(): sleep_duration = 60 while sleep_duration > 0: time.sleep(1) sleep_duration -= 1 then timer_thread.cancel() works as expected but it gives an error as I do it as I have provided in the question. @elano7 – Asmat Ullah Khan Sep 19 '22 at 03:51
  • before i changed my code it was doing what I wanted to do. first, let me explain what my code has to do. we have 60 sec of gameplay. we have to press spacebar when we see the target number on the screen and have 1 sec before the number changes. we have to get target 5 times in 60 sec and if we get that in time, then the game should stop the timer and the line timer_thread.cancel() was doing it, then I changed the code to send time to display the remaining time through Game class. and then this error started appearing @KarlKnechtel – Asmat Ullah Khan Sep 19 '22 at 04:25

2 Answers2

2

Since the Thread class has no cancel method, it should always produce the AttributeError.

Are you perhaps confusing it with a Timer (which is a subclass of Thread)? That does have a cancel method.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
1

with all your help I was able to find my answer @Guy gave the link to the question from which I manage to get the answer

def the_hunt(target_number, player):
    stop_threads = False
    timer_thread = Thread(target=timer, args=(player, lambda: stop_threads))
    while True:
        # when time is zero sec it will terminate the game
        if not timer_thread.is_alive():
            print("The Hunt is Over. You ran out of time!")
            player.game_status(False)
            return False
        
        # check for the win
        if player._score == player.win_points:
            #timer_thread.cancel()
            stop_threads = True
            player.game_status(True)
def timer(obj: Game, stop):
    sleep_duration = 60
    while sleep_duration > 0:
        time.sleep(1)        
        obj.duration_reduce(1)
        sleep_duration -= 1
        if stop():
            break   

it is not giving me the error and it is working as I wanted