4

I'm new in Python. This is my code:

import logging
logging.basicConfig(level = logging.DEBUG, format = "[%(levelname)s] (%(threadName)-10s) %(message)s")
import threading
import time

def thread1():
    for i in range(0, 10, 2):
        logging.debug(i)
        time.sleep(1)


def thread2():
    for i in range(10, 20):
        logging.debug(i)
        time.sleep(1)

t1 = threading.Thread(name = "thread1", target = thread1)
t1.setDaemon(True)
t1.start()

t2 = threading.Thread(name = "thread2", target = thread2)
t2.setDaemon(True)
t2.start()

When I copied the code and pasted it into a python command line prompt, I got the following result.

enter image description here

As you can see, the 2 threads already completed but it seems the program does not exit ( I did not get the command prompt back). I think it relates to my code that did not property end the threads. Is that true? How to fixed this issue? Thanks.

Just a learner
  • 26,690
  • 50
  • 155
  • 234
  • See my answer to the question [how to start and stop thread](http://stackoverflow.com/questions/15729498/how-to-start-and-stop-thread/15734837#15734837). – martineau Oct 14 '13 at 16:25
  • afaik you just need to call `thread_1.join()` in your main program – Joran Beasley Oct 14 '13 at 16:39
  • 1
    thread.setDaemon() is deprecated, use thread.daemon = True (or pass it as a keyword arg when creating the thread). See the doc: http://docs.python.org/2/library/threading.html#threading.Thread.daemon – ThinkChaos Oct 14 '13 at 16:43

2 Answers2

4

Actually, the command prompt returned in the line >>> [DEBUG] (thread1 ) 2 and was ready for more input. The deamon threads ran in the background, printed the the stuff they were supposed to and finally completed. You didn't need to type ctrl-c at all. You could have just entered another python command.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

If I remember correctly -- You need to use threading.Event to stop a thread. For example:

self.stoprequest = threading.Event()

""" When self.stoprequest.set() is called,
it sets an event on the thread - stopping it.""" 
self.stoprequest.set()

So if you create a threading.Event() on each thread you start you can stop it from outside using instance.set()

You can also kill the main thread from which the child threads were spawned :)

actionseth
  • 27
  • 3
  • 1
    Not really. Threads don't have a standard way to exit. You can implement some sort of command structure like Event but it only works if the thread has been coded to use it. An alternate example is a Queue and a special message the thread code recognizes to terminate. – tdelaney Oct 14 '13 at 16:56
  • Not true. Unless the target thread is checking stoprequest periodically, or blocked `wait`ing on that specific Event, setting the event doesn't do anything. The only reliable way to terminate a (non-daemon) thread in python is for the thread itself to detect when it is time to stop and do so. You can use `threading.Event` objects as one way to implement that, but they key is that the thread needs to cooperate. – Evan Oct 15 '13 at 21:46
  • You're absolutely right, I was not thorough in my answer. My past experience is using a while(threading_event.is_set()) - as a thread can not be stopped. It stops being alive when its run() method terminates – either normally, or by raising an unhandled exception. – actionseth Oct 18 '13 at 16:00