[py3]--An example of understanding multithreading and daemon

Source: Internet
Author: User

Understand

(Today has been a long time, thank Junqi greatly help understand, a summary of the words to solve their doubts) (the following summary is basically our dialogue)

1. The process is equivalent to a machine, multi-process is equivalent to multiple machines at the same time running, multi-threaded is multiple tasks on the same machine alternately run, you use a period of time, I spend a period of time.

2. So processes and threads are not necessarily connected.

3. However, regardless of the process or thread, it is generally tied to the task, the task ends, and the process and thread are destroyed.

4. If the process is likened to a machine, daemon understood to be the guardian of this machine, and once the Daemon guardian is over, the process of the machine is down.

5. It can be said that there will be several threads in a process, the end of all threads, then there is no need for the process to exist, it will end.

6. Daemon This translation is a bit misleading, it does not protect anything, he provides some basic services, like maintaining the network connection, the most basic function, the daemon interpretation as a service thread, may be good to understand some.

 def   music ():  for  i in  range (2  Logging.info ("I Listening Music ({})". Format (i)) Sleep Logging.info ("End music ({})". Format (i)) 
     def   Movie ():  for  i in  range (2  Logging.info ("I watching Movie ({})". Format (i)) Sleep (Logging.info) ("End Movie ({})". Format (i)) thr EADS  =[]t1  =threading. Thread (Target=music) threads.append (t1) T2  =threading. Thread (Target=movie) threads.append (T2)  

Example-1

Do not set daemon! Draw the main process and the run timeline of music, movie two threads as follows:

if __name__=='__main__':     forTinchThreads:# T.setdaemon (True)T.start () time.sleep (5)========= Run result =========2017-09-18 17:48:16 [Thread-1] I Listening music (0)2017-09-18 17:48:21 [Thread-2] I watching movie (0)2017-09-18 17:48:26 [Thread-1] End Music (0)2017-09-18 17:48:26 [Thread-1] I listening music (1)2017-09-18 17:48:36 [Thread-1] End Music (1)2017-09-18 17:48:36 [Thread-2] End Movie (0)2017-09-18 17:48:36 [Thread-2] I watching movie (1)2017-09-18 17:48:51 [Thread-2] End Movie (1)

Example-2

Set music and movie to daemon! When you set both of them as daemon threads, main is the only foreground thread. Key point: All the daemon threads terminate immediately after the foreground thread is all over! The run time axis is as follows:

if __name__ ' __main__ ' :      for inch Threads:        t.setdaemon (True)   # is set to Daemon #        t.start ()        time.sleep (5)         ========= Run results =========2017-09-18 18:05:38 [Thread-1] I listening music (0)2017-09-18 18:05:43 [ Thread-2] I watching movie (0)2017-09-18 18:05:48 [Thread-1] End music (0)2017-09-18 18:05:48 [ THREAD-1] I Listening music (1)

[py3]--An example of understanding multithreading and daemon

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.