3

I am referring to this Simple threading event example .

More specifically, this piece of code:

for i in range(4):
     t = threading.Thread(target=worker)
     t.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
     t.start()

From what I understand this created 4 threads that will be used. As I am more familiar with C++ and C# I am wondering about cleanup. Can I just leave these threads open or is there a proper way of 'closing'/disposing them? Please do not misread this as wanting to kill the thread. I am just wondering that when all work is completed is there a proper way of cleaning up.

Community
  • 1
  • 1
Talib
  • 335
  • 3
  • 15
  • You don't need to free up or otherwise manually dispose of the threads created. In the example they will block when the queue is empty and effectively quit running. – martineau Mar 14 '17 at 11:08
  • @martineau I know this thread is old, but does that mean that any thread will dispose itself once done running the function set as target? How about threads spawned by the thread and etc.? – Jeppe Jul 10 '19 at 12:01
  • @Jeppe: Yes, I believe so (if it's a daemon). Try it yourself and see. – martineau Jul 10 '19 at 12:14

0 Answers0