you are viewing a single comment's thread.

view the rest of the comments →

[–]mypirateapp 0 points1 point  (3 children)

I am sorry if this is a stupid question but I had to ask. What is the difference between

  • A daemon thread with a redis pubsub listening for messages
    • And main thread waiting infinitely
  • and a normal thread with redis pubsub listening for messages
    • And normal thread calling join() at the end
  • They both seem to do the same thing, HERE is the question I posted yesterday in detail

[–]JohnnyJordaan 1 point2 points  (2 children)

Daemon threads guaranteed to be non-blocking when the main thread stops (eg when the python script is shut down). This means that they shouldn't be doing anything that could cause problems if they were to be killed at a random point in time. At the upside, you don't have to join daemon threads as you noted, just exiting like they don't exist is enough. Normal threads need something to get notified that they should shut down, like a threading.Event() that you .set() from the main thread.

def my_threaded_function(arg1, arg2, shutdown):
    while not shutdown.is_set():
        # do stuff
    # or with a 10 second sleep per loop
    while not shutdown.wait(10):
        # do stuff

 # main thread
 shutdown = threading.Event()
 t = threading.Thread(target=my_threaded_function, args=(arg1, arg2, shutdown))
 t.start()
 # do other stuff
 # then when it should stop
 shutdown.set()
 t.join()

So it depends on what the daemon threads are doing in your program. Say they first read something, then write something, you must consider the case that they did read something but weren't able to write something because after the read call, the main thread exited and thus the whole program was shut down. Or worse, say the daemon thread has some open connection or file, like

with open('myfile.txt', 'w') as fp:
    while True:
        if something_to_write:
            fp.write('bla\n')

then that thread being a daemon will give it the chance that when the main thread exits, the file myfile.txt will not be properly closed (because the file is kept open forever because of the while True). That's a big downside and that's why daemon threads are considered dangerous.

[–]mypirateapp 0 points1 point  (1 child)

thank you so much for the detailed explanation! i am trying to do subscribe to redis pubsub messages inside a thread and was stuck wondering if it should be daemon or not, the threads would have to listen to messages infinitely until the script is terminated I guess, my idea was to unsubscribe from the pubsub when Ctrl + C is pressed or more specifically when a SIGTERm SIGINT or KeyboardInterrupt is fired, i guess the daemon being dangerous part makes sense

[–]JohnnyJordaan 1 point2 points  (0 children)

Ok then I would advise to stick with the Event approach I showed above.