0

recently i met a problem, I call a child thread in the main thread to do something may be time consuming. But i don't want to wait in main thread for the child to terminate for too long time.

so i wrote code like this:

thread=threading.Thread(target=some_func,args=(some_ars,))
thread.start()
thread.join(10.0)  #waiting for just 10 seconds
...
sys.exit(return_code)

but i found when the child last longer than 10s, after the main thread exit,the child thread still doing its work.It's that OK? I heard when the main thread exit all its child threads will exit.

Ke Lu
  • 129
  • 3
  • 16
  • I found some docs in stack overflow saying sys.exit() just as thread.exit(), – Ke Lu Sep 06 '16 at 07:03
  • This is well known. The interpreter exists, when the the last [non-daemon](https://docs.python.org/2/library/threading.html#threading.Thread.daemon) thread exits. If you want your thread to die when the main thread exits, you need to set `daemon = True`. – dhke Sep 06 '16 at 07:14

0 Answers0