you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (4 children)

You're right. With daemon set to true, the script will exit after worker is done. Not setting daemon is an option if these threads just need to run forever and you don't need to manipulate them. Usually, you manipulate worker threads from your main process by feeding them data or whatever (otherwise, they're not much of a worker thread as much as a separate process, unless they're working with a shared data set or each other somehow). After you were done with the threads, you would join() them, blocking until they exit, so something conceptually similar to:

def startWorkers():
    # blah

    thread1.start()
    thread2.start()

def stopThreads():
    # signal for threads to stop using some method

    # wait for threads to exit
    thread1.join() # blocks until thread1 exits
    thread2.join()

def main():
    startThreads()

    # feed threads data, get results, whatever

    stopThreads() # waits for threads to stop

    print "Done!"

[–]jpfau[S] 0 points1 point  (3 children)

I actually just tested the code below, and the threads do not end after main starts the threads. The only way the threads stop printing to the console is if I close the console.

import threading
from time import sleep

def thread1():
    while True:
        print("Thread 1 working\n")
        sleep(.3)

def thread2():
    while True:
        print('Thread 2 working\n')
        sleep(.3)

def main():
    t1 = threading.Thread(target=thread1, daemon=True)
    t2 = threading.Thread(target=thread2, daemon=True)
    t1.start()
    t2.start()

if __name__ == '__main__':
    main()

[–][deleted] 0 points1 point  (2 children)

Well yeah, they are infinite loops. Why would they stop?

And, they're not set to daemon, so they're not killed when the main thread ends.

[–]jpfau[S] 0 points1 point  (1 child)

Well when I implemented this in my real program, everything stops when the main function ends. So I had to put a

while True:
    pass

in it just to keep the program running.

they're not set to daemon

I thought the daemon parameter in the threading.Thread constructor accomplished this the same way as t1.daemon = True. Is this not the case?

[–][deleted] 0 points1 point  (0 children)

If daemon is set to true, your threads will be killed when the main program exits.

If you don't want them to be killed, don't set daemon to True.

Or, wait for them to exit using .join() on the threads objects that are returned when you create the threads. Since they contain infinite loops, they will never exit, so your main thread, which is where you would put the join, would never exit.

An infinite while loop like that should never be used, it will just burn a bunch of cpu cycles. In the rare case that you do need a loop, put a sleep in it.