Python note 8-multithreaded threading Package

Source: Internet
Author: User

Tag: STC class Log Min implements inheritance of 9.png parent lock

Objective

Winter arrived, the small friends like to eat hot pot with the best friend, then this multi-person at the same time eat hot pot scene How to use Python to achieve it?

Execute function

1. Write an executive function to achieve something, different people eat hot pot with a parameter people instead.

# Coding=utf-8 Import Threading Import  Time def Chihuoguo (People):     Print ("%s small pot-eating partner-Lamb:%s" % (Time.ctime (), people))    Time.sleep (1)     Print ("%s small pot-eating partner-Fish ball:%s" % (Time.ctime (), people))

Rewrite threading. Thread

1. Use the threading module to create the thread directly from the threading. Thread inherits, and then overrides the __init__ method and the Run method

#Coding=utf-8ImportThreadingImport TimeclassMyThread (Threading. Thread):#inherits the parent class threading. Thread    def __init__(self, people, name):" "rewrite threading. Thread initialization Content" "Threading. Thread.__init__(self) self.threadname=name Self.people=peopledefRun (self):#write the code you want to execute into the run function. The thread runs the run function directly after it is created        " "overriding the Run method" "        Print("Start Thread:"+self.threadname) Chihuoguo (self.people)#Perform Tasks        Print("QQ Exchange Group: 226296743")        Print("End Thread:"+ self.name)

Start and run differences

The 1.start () method starts the thread activity.

It can only be called once for each thread object, and it arranges the object to call the Run () method (not the current thread) in an additional separate thread.

When the method is called more than once in the same thread object, a RuntimeError (run-time error) is introduced.

The 2.run () method represents the method of thread activity.

You can override this method in a subclass. The standard run () method invokes the Tunable object passed to the object's constructor as the target parameter, and if so, the order and keyword parameters are obtained from args and Kargs, respectively.

Reference Code
#Coding=utf-8ImportThreadingImport TimedefChihuoguo (people):Print("%s Small hot pot Partner-Lamb:%s"%(Time.ctime (), people)) Time.sleep (1)    Print("%s Small pot-eating partner-Fish ball:%s"%(Time.ctime (), people))classMyThread (Threading. Thread):#inherits the parent class threading. Thread    def __init__(self, people, name):" "rewrite threading. Thread initialization Content" "Threading. Thread.__init__(self) self.threadname=name Self.people=peopledefRun (self):#write the code you want to execute into the run function. The thread runs the run function directly after it is created        " "overriding the Run method" "        Print("Start Thread:"+self.threadname) Chihuoguo (self.people)#Perform Tasks        Print("QQ Exchange Group: 226296743")        Print("End Thread:"+self.name)#Create a new threadThread1 = MyThread ("xiaoming","Thread-1") Thread2= MyThread ("Xiaowang","Thread-2")#Open ThreadThread1.start () Thread2.start () Time.sleep (0.5)Print("Exit main thread")

Operation Result:

Start Thread:Thread-1WedJan 17 16: 33: 44 2018 Small Pot-eating partnerLambXiaoming Start Thread:Thread-2Wedjan 16:33 :44 2018 Eat Hotpot's little partner -Lamb: xiaowang exit main thread wed jan 16:33 :45 2018 Eat Hotpot's little partner -Fish balls: xiaomingwed jan 17 16 :33:45 2018 small partner to eat hotpot -fish Balls: xiaowang< Span class= "Hljs-selector-tag" > End thread: thread-1 End Thread: thread-2            

Note: There is a problem with running the results here, the main thread has exited, and the child threads Thread-1 and Thread-2 are still running. This is the daemon thread that needs to be talked about later ...

Python note 8-multithreaded threading Package

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.