Python threads: Scheduling of threads-Daemon threads

Source: Internet
Author: User

Python thread: The thread's dispatch-the daemon thread is basically different from the normal thread, and the method Setdaemon (true) that invokes the thread object can be set as the daemon thread. The recommended use of Thread.demon = True in Python is to use this method to detect that the data legitimacy daemon uses less, but is not useless, for example, Python's garbage collection, memory management and other threads are daemon threads. There is in the database application, the use of the database connection pool, the connection pool itself contains a lot of background threads, monitoring the number of connections, time-out, status, and so on. Detailed description of the Setdaemon method:
#attributes are encapsulated to make it easier to judge the legitimacy of incoming data values@daemon. Setter#will self._daemonic    defDaemon (self, daemonic):if  notself._initialized:RaiseRuntimeError ("thread.__init__ () not called")        ifSelf._started.is_set ():RaiseRuntimeError ("cannot set daemon status of active thread") Self._daemonic=Daemonic#determine if this thread is a daemon thread    defIsdaemon (self):returnSelf.daemon#Modifying the Daemonic property value by method is not recommended    defSetdaemon (Self, daemonic): Self.daemon= Daemonic

1 #Encoding:utf-82 #Author: "Richie"3 #date:8/29/20174 #python threads: Scheduling of threads-Daemon Threads5 6  fromThreadingImportThread,timer7 Import Time8 9 classMycommon (Thread):Ten  One     defRun (self): A          forIinchRange (1,5): -t = Timer (7, self.print_msg, args=(i,)) - T.start () the  -     defprint_msg (self,i): -         Print("Thread 1 Section"+ STR (i) +"times to execute! ") -  + classMydaemon (Thread): -     """ + The daemon thread cannot be re-new thread A     """ at     defRun (self): -          forIinchRange (9999999): -             Print("Background Thread Section"+ STR (i) +"times to execute! ") -Time.sleep (2) -  - if __name__=='__main__': inT1 =Mycommon () -t2 =Mydaemon () toT2.deamon = True#Set as daemon thread + T1.start () -T2.start ()

D:\Python\Python3.6\python.exe d:/python/pythonprogram/day35/ case study. PY background thread executes for the NO. 0 time! The background thread executes for the 1th time! The background thread executes for the 2nd time! The background thread executes for the 3rd time! Thread 1 executes for the 1th time! Thread 1 executes for the 2nd time! Thread 1 executes for the 3rd time! Thread 1 executes for the 4th time! Process finished with exit code 0

From the above execution results can be seen: the foreground thread is guaranteed to execute, the background thread has not finished execution and exited. In fact: The Python interpreter determines whether the program execution end is the standard of all the front line octave completed, regardless of background thread state, so in the use of the background county must pay attention to this problem.

Python threads: Scheduling of threads-Daemon threads

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.