1

I have a simple call to a thread:

aThread = threading.Thread(target=singleDriverThread, args=(d[0]))

and then I start it (aThread.start)

when I need to stop the thread I do:

aThread.join()

but the thread keep running.. Help?

user1952500
  • 6,611
  • 3
  • 24
  • 37
Adam
  • 299
  • 1
  • 4
  • 19
  • You simply cannot stop threads using `threading` API, you have to implement your own algorithms for this. `join` just waits for the thread to exit, blocking the current thread, so it doesn't send any kind of signal to `aThread`, telling it to terminate. – ForceBru Jan 16 '17 at 18:35
  • Possible duplicate of [Is there any way to kill a Thread in Python?](http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python) – Tagc Jan 16 '17 at 18:37
  • "join" is not "kill". Calling `.join()` just means that the one thread will wait for the other. – dsh Jan 16 '17 at 18:38

2 Answers2

1

The thread will continue to run until the callable singleDriverThread returns.

Example:

If your callable singleDriverThread looks like this the code will never stop:

def singleDriverThread():
    while True:
        # do stuff in eternal loop
        pass

But if singleDriverThread instead looks like this:

def singleDriverThread():
     # do stuff
     return

then the callable will return and the thread will join with the rest of your code.

cyber-cap
  • 265
  • 2
  • 9
1

If you want to stop a thread, you should either kill or signal the thread. This SO answer and this SO answer deal with stopping the thread. The join method only waits for the thread to exit.

In general, to make a thread stop gracefully, it is useful to have a communication channel with it so that a stop message / signal can be passed.

Community
  • 1
  • 1
user1952500
  • 6,611
  • 3
  • 24
  • 37