0

So the idea is: the program starts a thread, then when the same thread is called (started) again, the previous thread needs to stop and execute the recent started thread. How can that be done in python. Should I try to kill the running thread? Or is there a better way?

nanoxx
  • 1
  • Hard to imagine what you're talking about. Can you add some code to your question to illustrate it? – Solomon Slow Aug 25 '22 at 15:06
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 25 '22 at 15:11
  • `Thread` doesn't have method to kill it. If thread runs some loop `while True` then you could use `while running` with global variable `running = True` and later set `running = False` to stop this loop inside thread. – furas Aug 25 '22 at 18:06
  • as I remember there was similar question on Stackoverflow and one of answer suggested to use some system function to stop thread. Other suggested to add function which generate some error and this could also stop thread. Maybe if you search on Stackoverflow `python kill thread` then maybe find it. – furas Aug 25 '22 at 18:09
  • @SolomonSlow sorry It's a long code, but the idea is I want that same Thread while running to stop completely and restart again when it's called again with new parameters. I just wanna know which methods I can do to achieve that. – nanoxx Aug 26 '22 at 05:50
  • @nanoxx, In Python, like in most threading systems, the thing that is called `Thread` is not re-useable. It can only be started one time. If you want to perform the same _task_ more than one time, then you either will have to create a new `Thread` to run it each time, or else you will have to create a thread that can perform the task more than once, at appropriate times. Maybe you should look in to using a [thread _pool_](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor). – Solomon Slow Aug 30 '22 at 17:03

0 Answers0