#πŸ”’ What happens when a Thread goes out of scope?

14 messages Β· Page 1 of 1 (latest)

hollow bridge
#

what happens when a Thread object goes out of scope in the main thread? Will it be killed or does it just continue running?

wet bisonBOT
#

@hollow bridge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

cosmic path
#

A thread will continue running until it's completed

hollow bridge
#

So if it goes out of scope and never returns, it will just do it's thing, but also use memory?

cosmic path
#

yup

hollow bridge
#

alright, thx

smoky topaz
#

if you pass it the daemon flag it will be killed when your main application exits

hollow bridge
#

so If I don't pass the flag, it will just exist further until I restart my pc?

#

hmmm....

A thread can be flagged as a "daemon thread". The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread.

hot chasm
#

meaning: if you got non-daemon threads, your python program stays alive, until all threads have finished, or you forcibly kill the process (which also kills the threads)

light river
#

Generally best to avoid daemon threads, find some way to have the thread check an Event/flag so you can stop it running and join it

#

You can use a with ThreadPoolExecutor if you want to couple thread lifetimes to a scope

wet bisonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.