Thread in Python

Source: Internet
Author: User

Reprinted from: http://blog.sina.com.cn/s/blog_9f488855010198vn.html

Correct or not verified

Some of the mechanisms of thread in Python are different from C + +: in C + +, when the main thread ends, its child threads are killed by default by the main thread. In Python, after the main thread ends, it waits for the child thread to end by default, and the mainline friend exits.

Python has two functions for thread management: Join and Setdaemon

    • join: if Threada.join () is called in a thread B, thread B will then run back Threada.join () after Threada ends.
    • Setdaemon: The main thread A starts child thread B and calls B.setdaemaon (True), the thread B is also killed when the main thread ends, the same as the default effect in C + +.

Here is an example:

1 #! /usr/bin/env Python 2   3 import Threading 4 Import time 5   6 class MyThread (threading. Thread): 7   def __init__ (self, threadname): 8     Threading. Thread.__init__ (self, name=threadname) 9     self.st = 2   def run (self):     time.sleep (self.st) 13     print Self.getname ()   def setst (self, T):     self.st = t,  def fun1 ():   T1.start (19)   print "Fun1 done"-  def fun2 ():   t2.start ()   print "fun2 done"  t1=mythread ("T1") T2=mythread ("T2") t2.setst (+); T2.setdaemon (True) fun1 () fun2 () print "Now u'll See Me"

Thread in Python

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.