I have a thread that gets executed when some action occurs. Given the logic of the program, the thread cannot possibly be started while another instance of it is still running. Yet when I call it a second time, I get a "RuntimeError: thread already started" error. I added a check to see if it is actually alive using the Thread.is_alive() function, and it is actually dead.
What am I doing wrong?
I can provide more details as are needed.
Threads cannot be restarted. You must re-create the Thread in order to start it again.
From the Python documentation:
start()
starts the thread's activity.
This 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.
If you derive a class from threading.Thread you can add a Thread.__init__(self) at the end of your run method and you'll be able to call start again and it'll automatically reinitialize itself when done.
You can try setting
thread._Thread__started = False
It isn't officially documented, so use it on your own risk! :)
来源:https://stackoverflow.com/questions/2822677/python-terminated-thread-cannot-restart