Search code examples
pythonpython-multithreading

Why won't this thread restart?


I have a program that I have written and I am trying to stop and then restart the thread function. I know an instance of a thread can only be used once, but since the thread is being called from a function, and it is not a global variable, it is essentially a new variable being declared as far as I understand this.

In the example code below I first start myThread via a function call. I then pause for a moment (for loop) to let myThread get running before I stop it. Next I call the function again to restart the myThread (not really a restart since it is a new instance) but it never restarts as you can see by the output. It also does not throw the dreaded "RuntimeError: threads can only be started once" exception, so I know I'm not going down that road. I've simplified what I am actually doing with this code example, which is behaving the same way as my actual code.

#test-thread.py

import os, threading

stopThread = False
option = ''

def threaded_function():

    global option, stopThread

    n = 0
    while not stopThread:
        n += 1
        if option == "started":
            print ("myThread is running ("+str(n)+")\n")
        if option == "restarted":
            print ("myThread is running again ("+str(n)+")\n")

def thread_control_function():

    global stopThread

    print ("Entered the thread_control function\n")
    if option == "started":
        print ("Starting myThread\n")
        myThread = threading.Thread(target=threaded_function)
        myThread.start()
        print("Started myThread\n")
    elif  option == "restarted":
        print("restarting myThread\n")
        myThread = threading.Thread(target=threaded_function)
        myThread.start()
        print("restarted myThread\n")
    elif option == "stopped":
        print ("Stopping myThread\n")
        stopThread = True
        print ("myThread is stopped\n")
    print ("Exiting the thread_control function\n")

# Clear the python console
os.system("clear")

option = "started"
thread_control_function()

for i in range(1,200000):
    pass

option = "stopped"
thread_control_function()

for i in range(1,200000):
    pass

option = "restarted"
thread_control_function()

for i in range(1,200000):
    pass

option = "stopped"
thread_control_function()

In my main program that I am working on I have a Stop Game button that sets the stopThread variable to true when I click the Stop Game button. It actually stops the game and resets all the game variables. I can the click the Start Game button and it behaves like I expect it to (it starts a new game). I am trying to use a Restart Button that sets stopThread to true, does not reset all the game variables, and I then start (restart) the game thread. I don't see why this is not able to start another thread (restart).


Solution

  • The stopThread flag was never reset. The thread_control_function should look like:

    def thread_control_function():
    
        global stopThread
    
        print ("Entered the thread_control function\n")
        if option == "started":
            print ("Starting myThread\n")
            myThread = threading.Thread(target=threaded_function)
            myThread.start()
            print("Started myThread\n")
        elif  option == "restarted":
            print("restarting myThread\n")
            stopThread = False
            myThread = threading.Thread(target=threaded_function)
            myThread.start()
            print("restarted myThread\n")
        elif option == "stopped":
            print ("Stopping myThread\n")
            stopThread = True
            print ("myThread is stopped\n")
        print ("Exiting the thread_control function\n")