Python-thread & Threading

Source: Internet
Author: User

# 1, no threading example #  uses the Time.sleep () function to demonstrate the work of the thread, which is mainly a comparison of the following threads. # time.sleep () requires a floating-point parameter to specify the time of sleep (in seconds). This is equivalent to the execution of a program that is suspended for a specified time. #  code Explanation: Two timers, loop0 sleep 4 seconds, LOOP1 () Sleep 2 seconds, they are in a process or thread, sequentially execute loop0 () and LOOP1 (), the total run time is 6 seconds. It may take some time to start the process. From time import sleep, ctime def loop0 ():       print  ' Start loop 0 at: ',  ctime ()       sleep (4)        print  ' Loop 0 done at: ',  ctime ()  def loop1 ():       print  ' Start loop 1 at: ',  ctime ()        sleep (2)       print  ' Loop 1 done at: ',  ctime ()  def main ():      print  ' Starting at: ',  CTime ()       loop0 ()       loop1 ()        print  ' All done at: ',  ctime ()  if __name__ ==  ' __main__ ':       main ()
# result:starting at: wed jul 26  23:10:41 2017start loop 0 at: wed jul 26 23:10:41 2017    # loop0 ()  sleep (4) loop 0 done at: wed jul 26 23:10:45  2017start loop 1 at: wed jul 26 23:10:45 2017   #  LOOP1 ()  sleep (2) Loop 1 done at: wed jul 26 23:10:47 2017all  done at: wed jul 26 23:10:47 2017[finished in 6.1s] 
# 2, avoid using the thread module #          python provides several modules for multithreaded programming, Includes thread, threading, and queue. #           (1)  thread module:  allows programmers to create and manage threads, It provides basic threading and lock support. #           (2)  threading module:  allows programmers to create and manage threads, which provides a higher level of The ability to manage more powerful threads. #           (3)  queue module:  Allows a user to create a queue data structure that can be used to share data among multiple threads. #           Simple Analysis below why do I need to avoid using the thread module? #           (1)   first, higher-level threading modules are more advanced, support for threading is better, and the use of attributes in the thread module may conflict with threading. #           (2)   Secondly, the synchronization primitives of the low-level thread module are few (actually only one), The threading module has many. #           (3)   Another reason is that thread has absolutely no control over when your process should end, when the main thread ends, All threads will be forced to end, without warning and without proper cleanup work. The threading module ensures that important sub-threads exit after the process exits. #           of course, the thread will still be explained for you to better understand the threads. However, we only recommend that experienced experts use the thread module when they want to access the underlying structure of the thread. And if you can, your first threaded application should use high-level modules such as threading whenever possible.


Python-thread & Threading

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.