Python Induction (11) _ Thread _threading. Thread

Source: Internet
Author: User
Tags local time

Summarize:
    1. The default parent thread runs out, and the child threads do not exit immediately, unlike THREAD.START_THREADXXXX
    2. The parent thread ran out, and did not quit, and had been there
    3. 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:
    1. 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
    2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.