5

I was making a simple Python game. I was trying to fix the following error:

RuntimeError: threads can only be started once

I have tried .cancel() the timer but that hasn't seem to be working, and I have made a if statement to see if the timer .is_alive before executing it. The console is throwing the error off at ball_char = play_timer.start().

def playball(state):
 batbox = [["@", "@", "@", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "@", "@", "@"]]
 play = "playing"
 play_timer = Timer(1.0, pitch(batbox))
 end_timer = Timer(6.0, pitch_end(play))
 play_timer.cancel()
 end_timer.cancel()
 pstate = "idle"
 inning = 1
 outs = 0
 pscore =0
 cscore = 0
 strikes = 0
 ball_row = 0
 ball_col = 0
 ball_char = "."
 while play == "playing":
     batbox = [["@", "@", "@", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "@", "@", "@"]]
     os.system('cls')  # on windows
     os.system('clear') # on linux / os x
     # Playing the game
     print "Press enter to start / hit the pitch"
     print_grid(batbox)
     input = raw_input("")
     pstate = "hitting"
     # Hitting
     if pstate == "hitting":
         ball_row = random_row(batbox) 
         ball_col = random_col(batbox)
         end_timer.start()
         while pstate == "hitting":
             batbox[ball_row][ball_col] = ball_char
             if play_timer.is_alive():
                 play_timer.cancel()
             else:
                 ball_char = play_timer.start()
 else:
     play_timer.cancel()
     end_timer.cancel()
     state = "mainmenue"
     return state
2
  • 1
    Means exactly what it says. If you want to start multiple threads, create multiple objects. Once you've started a thread once, you can't start it again, even if it's shut down or exited in the interim -- create a new Timer object instead. What's the mystery here? Oct 30, 2017 at 17:32
  • 1
    BTW, in general, a minimal reproducible example would have all the bits that are related to playing a game removed, and would focus only on providing the shortest possible code that demonstrates the behavior with Timers/threads you need help with. (Moreover, a proper MCVE will be code that anyone can copy-and-paste to see the problem themselves without any other setup or framework -- imports, setup code, etc -- needed; this isn't runnable as-is, and so is outside that definition). Oct 30, 2017 at 17:35

1 Answer 1

11
+100

The documentation for Threading.Thread (of which Timer is a subclass) states:

start() Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

Even if you cancel a thread (or timer), you still cannot call start() again. You need to create a new thread/timer object or it is an error.

2
  • 3
    So could I, redefine play_timer = Timer(1.0, pitch(batbox)) ? Or do I have to make an entirely new timer?
    – Handge
    Oct 30, 2017 at 20:09
  • 2
    @Handge Yes, that code would create a new timer that you could use.
    – mbrig
    Oct 30, 2017 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.