Summarize:
- The default parent thread runs out, and the child threads do not exit immediately, unlike THREAD.START_THREADXXXX
- The parent thread ran out, and did not quit, and had been there
- Thread startup is fast and does not cost much, less than 1 milliseconds
Code:
#-*-Coding:utf-8-*-"" "Learning Concurrent Linux Execution" "" from threading import threadfrom multiprocessing import Processimport Timeimport Osimport Psutildef Work(): Print ('%s,%f:sub begin%d '% (time.strftime ('%m:%s ', Time.localtime (Time.time ())), Time.time (), Os.getpid ())) Prin T "%s,%f:sub%d,%d,%d"% (time.strftime ('%m:%s ', Time.localtime (Time.time ())), Time.time (), Os.getpid (), Os.getppid (), Psutil. Process (Os.getpid ()). Num_threads ()) Time.sleep (5) print "%s,%f:sub%d,%d,%d"% (time.strftime ('%m:%s ', Time.localtim E (Time.time ())), Time.time (), Os.getpid (), Os.getppid (), Psutil. Process (Os.getpid ()). Num_threads ()) print ('%s,%f:sub end%d '% (time.strftime ('%m:%s ', Time.localtime (Time.time ())), Time.time (), Os.getpid ())) if __name__ = = ' __main__ ': print "%s,%f:main begin"% (Time.strftime ('%m:%s ', Time.localtime (t Ime.time ())), Time.time ()) print "%s,%f:main%d,%d,%d"% (time.strftime ('%m:%s ', Time.localtime (Time.time ())), Time.time (), Os.getpid (), Os.getppid (), Psutil. Process (Os.getpid ()). Num_threads ()) # Turn on child threadsT=thread (target=work) T.start ()print '%s,%f:main thread '% (time.strftime ('%m:%s ', Time.localtime (Time.time ())), Time.time ()) print "%s,%f:main%d,%d,%d "% (Time.strftime ('%m:%s ', Time.localtime (Time.time ())), Time.time (), Os.getpid (), Os.getppid (), Psutil. Process (Os.getpid ()). Num_threads () # Open subprocess # t=process (target=work) # T.start () # print "\nmain%d,%d,%d"% ( Os.getpid (), Os.getppid (), Psutil. Process (Os.getpid ()). Num_threads ()) # print (' \nmain process ') print "%s,%f:main End"% (Time.strftime ('%m:%s ', time.local Time (Time.time ())), Time.time ())
Output:
Format Description:
Hours: seconds, timestamp, < current process ID, [parent process ID, number of current process threads]>
[Email protected]:~/python# python test_threadprocess1.py
----------------------------------------------------------
03:04,1536397384.349529:main begin
03:04,1536397384.349605:main 3604,2692,1
03:04,1536397384.350838:sub begin 3604
03:04,1536397384.350939:main Threads
03:04,1536397384.351009:sub 3604,2692,2
03:04,1536397384.351310:main 3604,2692,2
03:04,1536397384.352097:main End
03:09,1536397389.357353:sub 3604,2692,2
03:09,1536397389.358372:sub End 3604
----------------------------------------------------------
Output Interpretation:
- The middle output portion of the yellow mark, which is very fast during the start of the sub-thread, indicates that the thread is starting fast
- Red Flag, when the logical code of the main thread runs out, but it still shows 2 threads, and the comparison begins, stating that the main thread did not exit
Python Induction (11) _ Thread _threading. Thread